Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I set variables to be used in scripts as well? They don't have to be global / system-wide, just the current session is sufficient. But for some reason they seem gone even if I run a script right from my current terminal.

Example:

foo=bar
echo "$foo"

output: bar

However if test.sh contains echo "$foo" and I do:

foo=bar
./test.sh

The output is empty. How do I set a scope for temporary variables with a terminal session that remains valid when executing scripts from that same session?

share|improve this question
    
You need to export them – Arkadiusz Drabczyk Apr 22 '16 at 14:20
up vote 3 down vote accepted

export foo=bar will set the global variable $foo to bar. You can also use foo=bar ./test.sh as a single command to set foo to bar for test.sh.

share|improve this answer
    
export foo=bar needs to be entered without spaces… or you'll get "-bash: export: `=': not a valid identifier" – Guido Apr 22 '16 at 14:47
    
Whoops, thanks. Fixed. – Kenny Apr 22 '16 at 14:49

When you run your shell script, it executes in a new shell instance, and does not inherit any variables instantiated in the interactive shell instance.

Specific variables can be inherited this way, called environment variables. You make a variable assignment an environment variable by using export, such as export foo=bar. This is the bash syntax, other shells may use env or some other method.

You can also cause the shell script to execute in the same shell instance by 'sourcing' it. You can do this with . test.sh (note the period) or using source test.sh. You can do this in your interactive session, or you can do this from within a shell script. This is really handy for creating shell "libraries", where you source in a set of shell functions or variable definitions from an external file. Super handy.

For instance:

#!/bin/bash
. /path/lib.sh
echo $foo # foo is instantiated in lib.sh
do_something # this is a shell function from lib.sh

where lib.sh is:

foo="bar"
do_something() {
echo "I am do_something"
}
share|improve this answer
    
It's worth mentioning that sourcing arbitrary scripts is not a good idea—you should fully understand what they may do to your environment first—but I agree that setting up a function "library" is a very handy use for sourceing. Nicely explained. – Wildcard Apr 22 '16 at 21:40

Another option could be to pass the variable to your script as an argument.

foo=bar
./test.sh $foo

The issue then becomes the contents of ./test.sh

It would have to echo $1 instead of echo $foo - the $1 indicating the first argument passed to the script.

share|improve this answer

You must run the assignment and echo test srcipt in the same shell.

root@ijaz-HP-EliteBook-8560p:~# foo=bar
root@ijaz-HP-EliteBook-8560p:~# cat test.sh 
   #!/bin/bash
   echo "$foo"
root@ijaz-HP-EliteBook-8560p:~# . test.sh 
bar

this is just one way of doing it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.