printenv命令来自英文词组“print environment”的缩写,其功能是用于显示系统环境变量与值。Linux系统中的环境变量多达几十个,在它们的共同协作下才有了用户的工作界面,例如PATH变量让命令得以被运行了、MAIL变量让邮件能够被接收了、HOME变量让用户有了家目录等。

语法格式:printenv [参数] [变量名]

常用参数:

--help显示帮助信息
--null使用空字符结束每一行
--version显示版本信息

参考示例

显示系统中所有的环境变量与值:

[root@linuxcool ~]# printenv
XDG_MENU_PREFIX=gnome-
LANG=en_US.UTF-8
GDM_LANG=en_US.UTF-8
HISTCONTROL=ignoredups
DISPLAY=:0
HOSTNAME=linuxcool.com
COLORTERM=truecolor
USERNAME=root
XDG_VTNR=2
SSH_AUTH_SOCK=/run/user/0/keyring/ssh
………………省略部分输出信息………………

显示指定环境变量的值:

[root@linuxcool ~]# printenv LANG
en_US.UTF-8
[root@linuxcool ~]# printenv HOME
/root
[root@linuxcool ~]# printenv MAIL
/var/spool/mail/root
[root@linuxcool ~]# printenv PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/root/bin

显示命令的帮助信息:

[root@linuxcool ~]# printenv --help
Usage: printenv [OPTION]... [VARIABLE]...
Print the values of the specified environment VARIABLE(s).
If no VARIABLE is specified, print name and value pairs for them all.

  -0, --null     end each output line with NUL, not newline
      --help     display this help and exit
      --version  output version information and exit

NOTE: your shell may have its own version of printenv, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/printenv>
or available locally via: info '(coreutils) printenv invocation'
Author

这篇优质的内容由TA贡献而来

刘遄

《Linux就该这么学》书籍作者,RHCA认证架构师,教育学(计算机专业硕士)。