Bash shebang选项-l [英] Bash shebang option -l

查看:55
本文介绍了Bash shebang选项-l的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用别人编写的脚本 test.sh ,开头是bash shebang:

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:

  • 如果我运行 TEST = hey ./test.sh ,我可以看到 TEST:hop hop 是变量<我的 .bash_profile
  • 中的code> TEST
  • 如果我在运行脚本之前 export TEST = hey ,这是相同的
  • 但是如果我删除 -l <​​/code>标志,则相同的命令将返回 TEST:hey ,正如我期望的那样
  • 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

有人可以解释这种行为吗?bash的帮助没有...帮助.

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

推荐答案

-l <​​/code>选项(根据 .bash_profile .由于您在 .bash_profile 中设置了 TEST 的值,因此在启动 bash 时,在命令行上设置的值将被覆盖.

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.

这篇关于Bash shebang选项-l的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆