Hướng dẫn nhanh vềenv
lệnh, được sử dụng để chạy lệnh và tương tác với các biến môi trường
Cácenv
lệnh có thể được sử dụng để chuyển các biến môi trường mà không cần thiết lập chúng trên môi trường bên ngoài (trình bao hiện tại).
Giả sử bạn muốn chạy ứng dụng Node.js và đặtUSER
biến đối với nó.
Bạn có thể chạy
env USER=flavio node app.js
vàUSER
biến môi trường sẽ có thể truy cập được từ ứng dụng Node.js thông qua Nodeprocess.env
giao diện.
Bạn cũng có thể chạy lệnh xóa tất cả các biến môi trường đã được thiết lập, bằng cách sử dụng-i
Lựa chọn:
env -i node app.js
Trong trường hợp này, bạn sẽ gặp lỗi nóienv: node: No such file or directory
bởi vìnode
không thể truy cập được lệnh, vìPATH
biến được shell sử dụng để tra cứu các lệnh trong các đường dẫn chung không được đặt.
Vì vậy, bạn cần phải vượt qua toàn bộ đường dẫn đếnnode
chương trình:
env -i /usr/local/bin/node app.js
Hãy thử với một đơn giảnapp.js
tệp với nội dung này:
console.log(process.env.NAME)
console.log(process.env.PATH)
Bạn sẽ thấy đầu ra là
undefined
undefinedYou can pass an env variable:
env -i NAME=flavio node app.js
and the output will be
flavio
undefinedRemoving the -i
option will make PATH
available again inside the program:

The env
command can also be used to print out all the environment variables, if ran with no options:
env
it will return a list of the environment variables set, for example:
HOME=/Users/flavio
LOGNAME=flavio
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
PWD=/Users/flavio
SHELL=/usr/local/bin/fish
You can also make a variable inaccessible inside the program you run, using the -u
option, for example this code removes the HOME
variable from the command environment:
env -u HOME node app.js
The env
command works on Linux, macOS, WSL, and anywhere you have a UNIX environment
Download my free Linux Commands Handbook
More cli tutorials:
- The Bash shell
- Introduction to Bash Shell Scripting
- The Fish Shell
- Shell, watch file content as it populates
- How to exit Vim
- UNIX Editors
- The UNIX Filesystem Commands
- Unix Shells Tutorial
- How to set an alias in a macOS or Linux shell
- A practical guide to Homebrew
- How to fix the xcrun invalid active developer path error in macOS
- The Command Line for Complete Beginners
- Introduction to Linux
- How to find the process that is using a port
- Linux commands: mkdir
- Linux commands: cd
- Linux commands: pwd
- Linux commands: rmdir
- Linux commands: ls
- Linux commands: mv
- Linux commands: cp
- Linux commands: less
- Linux commands: tail
- Linux commands: touch
- Linux commands: cat
- Linux commands: find
- Linux commands: ln
- Linux commands: ps
- Linux commands: echo
- Linux commands: top
- Linux commands: kill
- Linux commands: killall
- Linux commands: alias
- Linux commands: jobs
- Linux commands: bg
- Linux commands: fg
- Linux commands: type
- Linux commands: which
- Linux commands: whoami
- Linux commands: who
- Linux commands: clear
- Linux commands: su
- Linux commands: sudo
- Linux commands: chown
- Linux commands: chmod
- Linux commands: passwd
- Linux commands: open
- Linux commands: wc
- Linux commands: history
- Linux commands: du
- Linux commands: umask
- Linux commands: grep
- Linux commands: man
- Linux commands: uname
- Linux commands: sort
- Linux commands: uniq
- Linux commands: diff
- Linux commands: nohup
- Linux commands: df
- Linux commands: xargs
- Linux commands: gzip
- Linux commands: gunzip
- Linux commands: ping
- Linux commands: traceroute
- Linux commands: tar
- Linux commands: export
- Linux commands: crontab
- Linux commands: dirname
- Linux commands: basename
- Linux commands: printenv
- Linux commands: env
- A short guide to the ed editor
- A short guide to vim
- A short guide to emacs
- A short guide to nano
- Linux, no space left on device
- How to use Netcat