Shell Variables

An introduction to Bash Shell Variables

What are Shell variables?


Shell variables are simply variables that are defined each time a command is executed. These special variables contain information about the command that was executed and its arguments. As the line is scanned these values are assigned to special numerical variables commonly known as positional parameters. To illustrate this lets look at a simple script:



   
#!/bin/bash
printf "\nName of script $0\n"
printf "Parameters Passed: $1 $2 $3 $4 $5 $6\n"

The first line of our script indicates that this is a Bash Shell script. The second line uses the special variable "$0" to display the name of the executing script/command. The last line prints the output of any arguments that were passed when the script was executed:



john@john-desktop:~/scripts$ ./script01.sh first second third fourth fifth sixth 

Name of script ./script01.sh
Parameters Passed: first second third fourth fifth sixth

As we can see from the above output. The arguments "first second third fourth fifth sixth" were passed". The variables $1, $2, $3, $4, $5 and $6 were assigned these values accordingly. It is important to note that if you pass more than nine arguments, you will need to specify the variable between "{ }" braces. For example ${10}.

So far we have seen that as the line is scanned by the shell each command and argument are assigned a numeric variable. However, these are not the only variables that are created after each line scan. Theses are as follows:


Special Shell Variables


Variable Description
$1 $2 $3 Numeric variables that are assigned arguments passed
${nn} Numeric variables with values greater than 9
$0 Script Name
$# Number of command arguments
$@ All command arguments - List of separate items
$* All command arguments - A single item
$? Exit status of most recent command
$$ Process ID of current program/script

To illustrate some of these variables lets modify our example script as follows and then execute it:



john@john-desktop:~/scripts$ cat script01.sh 
#!/bin/bash

printf "\nName of script $0\n"
printf "Parameters Passed: $1 $2 $3 $4 $5 $6\n"
printf "number of arguments passed $#\n"
printf "$*\n"
printf "PID - Process ID is $$\n"
john@john-desktop:~/scripts$ ./script01.sh first second third fourth fifth sixth 

Name of script ./script01.sh
Parameters Passed: first second third fourth fifth sixth
number of arguments passed 6
first second third fourth fifth sixth
PID - Process ID is 4063


shift command


There is a special command called "shift" that allows us to discard the leftmost parameter. This allows us to discard the contents within $1 and replace it with the contents from $2. This functionality can be quite useful when processing data within a loop. To illustrate the "shift" command lets create a new test script with the following lines:



john@john-desktop:~/scripts$ cat script02.sh 
#!/bin/bash

printf "\nName of script $0\n"
printf "Parameters Passed: $1 $2 $3 $4 $5 $6\n"
printf "number of arguments passed $#\n"

shift 1
printf "Parameters Passed: $1 $2 $3 $4 $5 $6\n"

shift 1
printf "Parameters Passed: $1 $2 $3 $4 $5 $6\n"

shift 2
printf "Parameters Passed: $1 $2 $3 $4 $5 $6\n"

Now if we run this script, you will see the leftmost variable is discarded:



john@john-desktop:~/scripts$ ./script02.sh first second third fourth fifth sixth 
Name of script ./script02.sh
Parameters Passed: first second third fourth fifth sixth
number of arguments passed 6
Parameters Passed: second third fourth fifth sixth 
Parameters Passed: third fourth fifth sixth  
Parameters Passed: fifth sixth


eval command


The "eval" command instructs the shell to re-parse the line. This can be useful if you are assigning a value that has the same name as a variable. In the example below we try to assign var2 with var1 and then display our output.



john@john-desktop:~/scripts$ cat script03.sh 
#!/bin/bash

printf "\nName of script $0\n"

var1="First"
var2=var1
echo '$'$var2
eval echo '$'$var2

Name of script ./script03.sh
$var1
First

In the above example, notice that the output from the first echo command issued "$var1", however, when we used the "eval" command we were then able to display our value correctly.