Using the built-in Bash Special Variables

By | October 23, 2015

Bash Special Variables

Bash has a number of what they term ‘bash special variables’. Here is a list of the most useful ones I use fairly regular in scripts :

$? – Exit code

$# – Number of entered arguments/parameters to a script

$0 – Name of the script or shell executing the command

$1 (up to number of parameters entered) – Value of specified argument/parameter

$@ – Expands to be a lit of all the entered parameters

 

ok so a quick explanation of those bash special variables…

$? is used within scripts to determine the result from the previous command. For example if you enter an incorrect command immediately before you will get this :

# lss
-bash: lss: command not found
# echo $?
127

ls was the actual command I actually wanted to do which would show this :

# ls
wget-log
# echo $?
0

A number 0 in 99.9% of cases indicates the previous command was successful, anything else indicates a failure of some sort. The man pages for the command you are running may tell you more concise error numbers for that command so you can further customise your script.

$# is a particularly useful command I normally use for validating the number of parameters a user has entered to a script. For example if you have written a script which needs 2 parameters, and has an optional 3rd parameter as well then you can do this :

if [ $# -eq 3 ]
then
echo "3 parameters entered, continuing processing"
elif [ $# -eq 2 ]
then
echo "2 parameters entered, no date supplied so using today" # just example
elif [ $# -lt 2 ]
then
echo "You need to enter at least 2 parameters - exiting"
exit 1
fi

Clearly in that script the actions taken for each option can be tailored to your needs

Check out -  How to test if lftp has worked in bash script

$0 I generally use to form a base for log file names in conjunction with basename like this :

progname=`basename $0`
logfile="/tmp/logs/$progname.log"

$0 is the whole path to your script, basename takes just the script name part ignoring any directory names, then the logfile line determines where you are going to push your log entries to.

$1 (etc) is the value of a positional parameter. If you run a script like this as example :

./script1.bash 1 2 3 4

Inside script1.bash if you have this :

echo $2

Then you will see the value it displays is 2. Now you can see the relationship between $1 and $0 as all $0 is doing is displaying the first word of the command line you entered.

$@ can be used in a loop to rotate around all your entered parameters. Using the above script1.bash as example :

# cat script1.bash
#!/bin/bash

for i in $@
do
echo "Value : $i"
done

Run the script like this and here is the output :

# ./script1.bash 1 2 3 4 5
Value : 1
Value : 2
Value : 3
Value : 4
Value : 5

There are plenty of other Bash special variables that you may find useful, check out the ‘man bash’ help page to find them all or you can see them HERE.

Check out my other bash tips and tutorials HERE

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.