Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to source file from bash script

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.

like image 409
franki3xe Avatar asked Oct 27 '13 17:10

franki3xe


1 Answers

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 :)

like image 169
Nikksno Avatar answered Oct 06 '22 20:10

Nikksno