Unix Shell Script Variables
April 15, 2007 by Mark Marucot
Variables are use to store information. The syntax of declaring variables is shown below:
-
VAR=value
Note: There must be no spaces between the variable and the value. VAR=value will work but VAR=value will not. The reason is when the shell sees that the “=”, it assumes that it’s a variable assignment. If there is spaces before and after the “=”, it interpreted VAR as a command with two arguments “=” and “value”.
-
#!/bin/sh
-
MESSAGE=“Hello Unix”
-
echo $MESSAGE
In the sample above, the “Hello Unix” is passed to the variable MESSAGE and then displays “Hello Unix” using the echo command. Take note that the value passed to the variable is enclosed by double quotes because the value is a string with space between the words.
It is possible to use MESSAGE=Hello because it contains single word only.
-
#!/bin/sh
-
MESSAGE=Hello
-
echo $MESSAGE
Shell does not care about the type of variables. They can store strings, numbers, integers and real numbers, etc. This true because in reality variables are stored as strings.
Note: Though that special characters must be properly escaped to avoid interpretation by the shell.
Reading Variables
Values can be set to variables using the read command.
-
#!/bin/sh
-
echo What is your name?
-
read NAME
-
echo "Nice to meet you $MY_NAME"
Scope of Variables
The variables in shell script can be declared or can be undeclared. There will be no problem in using declared or undeclared variables. Undeclared variables contains null value since it’s not yet defined. The scope of variables can be global or local.
Local Variables
Local variables are variables that can be use within the program.
var1.sh
-
#!/bin/sh
-
echo “VAR1 contains :$VARIABLE”
-
VARIABLE=Hello
-
echo “VAR1 contains :$VARIABLE”
Try to save the code above and run it. The output is:
-
$./var1.sh
-
VAR1 contains :
-
VAR1 contains :Hello
Notice that the VAR1 has no value. When VAR1 is assigned by the value “Hello”, it display the value on the second line.
Run it.
-
$ VAR1=Hi
-
$./var1.sh
-
VAR1 contains :
-
VAR1 contains :Hello
VAR1 still has no value. This is because the scope of variable VAR1 is within the script shell instance. Every run, a new shell is spawned to execute the script. To fully use a variable in different programs, global variable should be declared.
Global Variables
Global variables are variables that can be shared in different programs. Global variables requires declaration to be used in other programs. See the syntax for global variables below and this should be executed in the Unix command line.:
-
export VARIABLE
Example:
-
$ export VAR1
-
$ VAR1=Hi
-
$ ./var1.sh
-
VAR1 contains :Hi
-
VAR1 contains :Hello
Note that in the script the value of VAR1 is changed. But this value will not override the value set in the shell.
-
$ echo $VAR1
-
Hi
-
$
To retain the value of VAR1 set inside the script, run the script using the “.” command.
-
$ export VAR1
-
$ VAR1=Hi
-
$ . ./var1.sh
-
VAR1 contains :Hi
-
VAR1 contains :Hello
-
$ echo $VAR1
-
Hello
-
$




Comments
Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!