在返回非交互式shell 之前 ,我在.bashrc的顶部有这个
FOO="BAR"; export FOO echo "HELLO WORLD" # If not running interactively, don't do anything [ -z "$PS1" ] && return我在我的homedirectory中有一个脚本test.sh:
#!/bin/bash echo "A" echo $FOO echo "B"我执行test.sh. 输出:
A B2个问题:
为什么我看不到$ FOO的价值? 为什么我没有看到“你好世界”?编辑:我认为带#!/ bin / bash的脚本会触发一个子shell,它会再次调用.bashrc,我错了吗?
编辑:即使我从其他主机调用脚本,我也看不到任何值。 即使这样,.bashrc也会被执行???
ssh remotehost "/home/username/test.sh"I have this at the very top of my .bashrc, before the return for non-interactive shells
FOO="BAR"; export FOO echo "HELLO WORLD" # If not running interactively, don't do anything [ -z "$PS1" ] && returnI have a script test.sh in my homedirectory with this:
#!/bin/bash echo "A" echo $FOO echo "B"I execute test.sh. The output:
A B2 Questions:
Why don't I see the value of $FOO? Why don't I see the "HELLO WORLD"?edit: I thought the script with #!/bin/bash triggers a subshell which will call the .bashrc again, am I that wrong?
edit: Even If I do call the script from another host I won't see any values. Not even then , the .bashrc will be executed???
ssh remotehost "/home/username/test.sh"最满意答案
.bashrc仅为非登录交互式shell自动获取。 通常,你会放. .bashrc . .bashrc靠近.bash_login文件的开头,以确保.bashrc来源于登录和非登录交互式shell。
.bashrc不会自动为非交互式shell提供源代码,例如执行shell脚本时启动的shell。
由于您从.bashrc导出FOO ,因此test.sh看到FOO具有空值的事实告诉我您正在从登录shell运行脚本。 从提示打印BAR echo $FOO ? 如果确实如此,我会感到惊讶,而test.sh却没有。
.bashrc is only sourced automatically for non-login interactive shells. Often, you would put . .bashrc near the beginning of your .bash_login file, to ensure that .bashrc is sourced for both login and non-login interactive shells.
.bashrc is not sourced automatically for non-interactive shells, such as those started when you execute a shell script.
Since you export FOO from .bashrc, the fact that test.sh sees FOO having a null value tells me that you are running the script from a login shell. Does echo $FOO from the prompt print BAR? I would be surprised if it did and test.sh did not.
更多推荐
发布评论