Bash shebang option -l

时光毁灭记忆、已成空白 提交于 2019-12-06 17:14:19

问题


I use a script, test.sh, written by someone else, the begins with a 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, hop being the value of variable TEST 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 returns TEST: hey, as I would have expected

Can someone please explain this behaviour ? The help of bash did not... help.


回答1:


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.



来源:https://stackoverflow.com/questions/20499596/bash-shebang-option-l

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!