I'm trying to source a file with an environment variable from my bash script, but it doesn't work.
This is the content of my script (test.sh
), which is located in ~/scripts/test.sh
.
#!/bin/bash
FILE_NAME=/tmp/source_file
touch $FILE_NAME
echo "export TEST=\"test\"" > $FILE_NAME
source $FILE_NAME
Then I use alias
in my ~/.bashrc
.
alias testScript=~/scripts/test.sh
But when I use my script testScript
, it didn't set the environment variable.
None of the other methods worked for me [source /path/to/file
vs . ./path/to/file
, alias, etc...], until, thanks to this tutorial I found that using the:
#!/usr/bin/env bash
shebang
instead of the simpler #!/usr/bin/env
one lets arguments pass on to the interpreter, which I think is the key here – see this document for more info.
In any event, if source commands in any form aren't working for you, try checking your shebang, that might be the problem :)
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