how How to use variables in your shell scripts. - Shell Scripting

Just running individual commands from the shell script is useful, but this has its limitations. Often you’ll want to incorporate other data in your shell commands to process information. You can do this by using variables. Variables allow you to temporarily store information within the shell script for use with other commands in the script.This section shows how to use variables in your shell scripts.

Environment variables

You’ve already seen one type of Linux variable in action. You can access these values from your shell scripts as well. The shell maintains environment variables that track specific system information, such as the name of the system, the name of the user logged in to the system, the user’s system ID (called UID), the default home directory of the user, and the search path used by the shell to find programs.

You can display a complete list of active environment variables available by using the set command:

$ set
HISTFILE=/home/rich/.bash history

You can tap into these environment variables from within your scripts by using the environment variable’s name preceded by a dollar sign. This is demonstrated in the following script:

$ cat test2
# display user information from the system.
echo "User info for userid: $USER"
echo UID: $UID
echo HOME: $HOME

The $USER, $UID, and $HOME environment variables are used to display the pertinent information about the logged-in user.The output should look something like this:

$chmod u+x test2
$ ./test2
User info for userid: rich
UID: 501
HOME: /home/rich

Notice that the environment variables in the echo commands are replaced by their current values when the script is run. Also notice that we were able to place the $USER system variable within the double quotation marks in the first string, and the shell script was still able to figure out what we meant. There is a drawback to using this method though. Look at what happens in this example:

$ echo "The cost of the item is $15"
The cost of the item is 5

That is obviously not what I intended. Whenever the script sees a dollar sign within quotes, it assumes you’re referencing a variable. In this example the script attempted to display the variable $1 (which was not defined), then the number 5. To display an actual dollar sign, you must precede it with a backslash character:

$ echo "The cost of the item is \$15"
The cost of the item is $15

That’s better. The backslash allowed the shell script to interpret the dollar sign as an actual dollar sign, and not a variable. The next section shows how to create your own variables in your scripts.

User variables

Besides the environment variables, a shell script allows you to set and use your own variables within the script. Setting variables allows you to temporarily store data and use it throughout the script, making the shell script more like a real computer program.

User variables can be any text string of up to 20 letters, digits, or an underscore character. User variables are case sensitive, so the variable Var1 is different from the variable var1. This little rule often gets novice script programmers in trouble.

Values are assigned to user variables using an equal sign. No spaces can appear between the variable, the equal sign, and the value (another trouble spot for novices). Here are a few examples of assigning values to user variables:

var4="still more testing"

the shell script automatically determines the data type used for the variable value. Variables defined within the shell script maintain their values throughout the life of the shell script but are deleted when the shell script completes. Just like system variables, user variables can be referenced using the dollar sign:

$ cat test3
# testing variables
echo "$guest checked in $days days ago"
echo "$guest checked in $days days ago"

Running the script produces the output:

$ chmod u+x test3
$--+++ ./test3
Katie checked in 10 days ago
Jessica checked in 5 days ago

Each time the variable is referenced, it produces the value currently assigned to it. It’s important to remember that when referencing a variable value you use the dollar sign, but when referencing the variable to assign a value to it, you do not use the dollar sign. Here’s an example of what
I mean:

$ cat test4
# assigning a variable value to another variable
echo The resulting value is $value2

When you use the value of the value1 variable in the assignment statement, you must still use the dollar sign. This code produces the output:

$ chmod u+x test4
$ ./test4
The resulting value is 10

If you forget the dollar sign, and make the value2 assignment line look like:


you get the following output:

$ ./test4
The resulting value is value1

Without the dollar sign the shell interprets the variable name as a normal text string, which is most likely not what you wanted.

The backtick

One of the most useful features of shell scripts is the lowly back quote character, usually called the backtick (`) in the Linux world. Be careful, this is not the normal single quotation mark character you are used to using for strings. Since it is not used very often outside of shell scripts, you may not even know where to find it on your keyboard. You should become familiar with it, because it’s a crucial component of many shell scripts (hint: on a U.S. keyboard, it is usually on the same key as the tilde symbol (∼)).

The backtick allows you to assign the output of a shell command to a variable. While this doesn’t seem like much, it is a major building block in script programming.

You must surround the entire command line command with backtick characters:


The shell runs the command within the backticks, and assigns the output to the variable testing. Here’s an example of creating a variable using the output from a normal shell command:

$ cat test5
# using the backtick character
echo "The date and time are: " $testing

The variable testing receives the output from the date command, and it is used in the echo statement to display it. Running the shell script produces the following output:

$ chmod u+x test5
$ ./test5
The date and time are: Mon Sep 24 20:23:25 EDT 2007

That’s not all that exciting in this example (you could just as easily just put the command in the echo statement), but once you capture the command output in a variable, you can do anything with it.

Here’s a popular example of how the backtick is used to capture the current date and use it to create a unique filename in a script:

# copy the /usr/bin directory listing to a log file
today=`date +%y%m%d`
ls /usr/bin -al log.$today

The today variable is assigned the output of a formatted date command. This is a common technique used to extract date information for log filenames. The +%y%m%d format instructs the date command to display the date as a two-digit year, month, and day:

$ date +%y%m%d

The script assigns the value to a variable, which is then used as part of a filename. The file itself contains the redirected output of a directory listing. After running the script, you should see a new file in your directory:

-rw-r--r-- 1 rich rich 769 Sep 22 10:15 log.070922

The log file appears in the directory using the value of the $today variable as part of the filename. The contents of the log file are the directory listing from the /usr/bin directory. If the script is run the next day, the log filename will be log.070923, thus creating a new file for the new day.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Shell Scripting Topics