“env”和“set”(在 Mac OS X 或 Linux 上)有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5657060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between "env" and "set" (on Mac OS X or Linux)?
提问by stupakov
I get similar results running "env" and "set". Set gives more results - is it a superset of env?
我在运行“env”和“set”时得到了类似的结果。Set 提供更多结果 - 它是 env 的超集吗?
The man page for set doesn't give any information. How do these commands work and what's the difference?
set 的手册页没有提供任何信息。这些命令是如何工作的,有什么区别?
采纳答案by intgr
Long story short: set
can see shell-local variables, env
cannot.
长话短说:set
可以看到外壳局部变量,env
不能。
Shells can have variables of 2 types: locals, which are only accessible from the current shell, and (exported) environment variables, which are passed on to every executed program.
Shell 可以有两种类型的变量:局部变量,只能从当前 shell 访问,以及(导出的)环境变量,传递给每个执行的程序。
Since set
is a built-inshell command, it also sees sees shell-local variables (including shell functions). env
on the other hand is an independent executable; it only sees the variables that the shell passes to it, or environment variables.
由于set
是一个内置的shell 命令,它也看到了 shell 局部变量(包括 shell 函数)。env
另一方面是一个独立的可执行文件;它只看到 shell 传递给它的变量或环境变量。
When you type a line like a=1
then a local variable is created (unless it already existed in the environment). Environment variables are created with export a=1
当您键入这样的行时,a=1
会创建一个局部变量(除非它已存在于环境中)。环境变量是用export a=1
回答by Stephen P
set
is a shell builtin, while env
is a program (/usr/bin/env)
set
是一个内置的 shell,env
而是一个程序 (/usr/bin/env)
set
does several things, but by itself it lists the environment variables. It can also set/toggle switches, such as set +x
or set -v
etc.
set
做了几件事,但它本身列出了环境变量。它还可以设置/切换开关,例如set +x
或set -v
等。
env
by itself lists the exportedenvironment variables, but can run a program in a modified environment
env
单独列出导出的环境变量,但可以在修改后的环境中运行程序
See man 1 env
for more information.
有关man 1 env
更多信息,请参阅。
回答by tim
If you want to limit the output of the set
command to variables only, you may run it in POSIX mode:
如果您想将set
命令的输出限制为仅变量,您可以在 POSIX 模式下运行它:
type -a env set
help set
(set -o posix; set) | nl
If you need finer control over listing specific variables, you may use Bash builtins such as declare
or compgen
, or some other Bash tricks.
如果您需要更好地控制列出特定变量,您可以使用 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