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" ] && return
I 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
B
2 Questions:
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With