问题描述
|
关闭。这个问题是题外话。它当前不接受答案。
解决方法
长话短说:
set
可以看到壳局部变量,env
不能看到。
Shell可以具有2种类型的变量:Locals(只能从当前Shell访问)和(导出的)环境变量,这些变量将传递给每个已执行的程序。
由于“ 0”是内置的shell命令,因此还可以看到shell局部变量(包括shell函数)。另一方面,env
是独立的可执行文件;它仅查看外壳传递给它的变量或环境变量。
当您键入类似于“ 4”的行时,将创建一个局部变量(除非环境中已存在该变量)。用ѭ5创建环境变量
,如果要将“ 0”命令的输出限制为仅变量,则可以在POSIX模式下运行它:
type -a env set
help set
(set -o posix; set) | nl
如果您需要更好地控制列出特定变量,可以使用Bash内置函数,例如declare
或compgen
或其他一些Bash技巧。
man bash | less -p \'-A action$\' # info on complete & compgen
# listing names of variables
compgen -A variable | nl # list names of all shell variables
echo ${!P*} # list names of all variables beginning with P
compgen -A export | nl # list names of exported shell variables
export | nl # same,plus always OLDPWD
declare -px | nl # same
declare -pr # list readonly variables
# listing names of functions
compgen -A function | nl
declare -F | nl
declare -Fx | nl
# show code of specified function
myfunc() { echo \'Hello,world!\'; return 0; }
declare -f myfunc
,set
是内置的shell,env
是程序(/ usr / bin / env)
set
做几件事,但它本身列出了环境变量。它还可以设置/切换开关,例如set +x
或set -v
等。
ѭ1本身列出了导出的环境变量,但可以在修改后的环境中运行程序
有关更多信息,请参见ѭ17。