Bash shebang 选项 -l
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20499596/
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
Bash shebang option -l
提问by Emmanuel
I use a script, test.sh
, written by someone else, the begins with a bash shebang:
我使用test.sh
其他人编写的脚本,以 bash shebang 开头:
#!/bin/bash -l
...
echo TEST: $TEST
From what I could see, this has an effect on variables used inside the script:
据我所知,这对脚本中使用的变量有影响:
- if I run
TEST=hey ./test.sh
, I can seeTEST: hop
,hop
being the value of variableTEST
in my.bash_profile
- this is the same if I
export TEST=hey
before running the script - but if I remove the
-l
flag, the same command returnsTEST: hey
, as I would have expected
- 如果我跑
TEST=hey ./test.sh
,我可以看到TEST: hop
,hop
作为变量的值TEST
在我.bash_profile
- 如果我
export TEST=hey
在运行脚本之前,这是一样的 - 但是如果我删除
-l
标志,则返回相同的命令TEST: hey
,正如我所期望的
Can someone please explain this behaviour ? The help of bash did not... help.
有人可以解释这种行为吗?bash 的帮助没有...帮助。
回答by dg99
The -l
option (according to the man page) makes "bash act as if it had been invoked as a login shell". Login shells read certain initialization files from your home directory, such as .bash_profile
. Since you set the value of TEST
in your .bash_profile
, the value you set on the command line gets overridden when bash
launches.
该-l
选项(根据手册页)使“bash 的行为就好像它已被作为登录 shell 调用一样”。登录 shell 从您的主目录读取某些初始化文件,例如.bash_profile
. 既然你设置的值TEST
在你的.bash_profile
,你在命令行上设置的值被重写时bash
启动。