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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:56:51  来源:igfitidea点击:

Bash shebang option -l

bashshellshebang

提问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 see TEST: hop, hopbeing the value of variable TESTin my .bash_profile
  • this is the same if I export TEST=heybefore running the script
  • but if I remove the -lflag, the same command returns TEST: hey, as I would have expected
  • 如果我跑TEST=hey ./test.sh,我可以看到TEST: hophop作为变量的值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 -loption (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 TESTin your .bash_profile, the value you set on the command line gets overridden when bashlaunches.

-l选项(根据手册页)使“bash 的行为就好像它已被作为登录 shell 调用一样”。登录 shell 从您的主目录读取某些初始化文件,例如.bash_profile. 既然你设置的值TEST在你的.bash_profile,你在命令行上设置的值被重写时bash启动。