use*_*279 31 linux bash shell shell-script
在 Linux shell 中设置变量时set
,env
、declare
和之间有什么区别export
,例如bash
?
Tra*_*iet 14
首先,你必须明白这environment variables
和shell variables
不是一回事。
然后,您应该知道外壳具有控制其工作方式的属性。这些属性既不是环境变量也不是 shell 变量。
现在,开始回答你的问题。
env
: 不带任何选项,显示当前环境变量及其值;但是可用于为带有-i
标志的单个命令设置环境变量set
: 不加选项,显示每个shell变量的名称和值*~来自man set
rhel中运行;也可用于设置shell 属性。此命令不设置环境或 shell 变量。declare
: 不带任何选项,同env
; 也可以用来设置shell变量export
: 使shell 变量成为 环境变量简而言之:
set
不设置 shell 或环境变量env
可以为单个命令设置环境变量declare
设置 shell 变量export
使 shell 变量成为环境变量NOTE
declare -x VAR=VAL
创建 shell 变量并导出它,使其成为环境变量。
似乎 set 和 declare 略有不同, set 更强大。
参见https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins下的“声明” :“声明变量并赋予它们属性。如果没有给出名称,则显示变量的值反而。
在https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin下设置“set” * set:“这个内置函数太复杂了,值得拥有它自己的部分。set 允许你更改 shell 选项的值并设置位置参数,或显示 shell 变量的名称和值。”
ENV 是 Bash 中的环境变量:https : //www.gnu.org/software/bash/manual/bash.html#Bash-Variables env 是 Linux 命令。我认为这是一个很好的参考:https : //unix.stackexchange.com/questions/103467/what-is-env-command-doing
我认为这是导出的一个很好的解释:http : //www.unix.com/302531838-post2.html
另外:https : //www.gnu.org/software/bash/manual/bash.html#Bourne-Shell-Builtins * export(来自 Bourne):“标记要传递给环境中子进程的每个名称。”
从上面的 URL 借用代码:
root@linux ~# x=5 <= here variable is set without export command
root@linux ~# echo $x
5
root@linux ~# bash <= subshell creation
root@linux ~# echo $x <= subshell doesnt know $x variable value
root@linux ~# exit <= exit from subshell
exit
root@linux ~# echo $x <= parent shell still knows $x variable
5
root@linux ~# export x=5 <= specify $x variable value using export command
root@linux ~# echo $x <= parent shell doesn't see any difference from the first declaration
5
root@linux ~# bash <= create subshell again
root@linux ~# echo $x <= now the subshell knows $x variable value
5
root@linux ~#
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16596 次 |
最近记录: |