Doctor Bob
Linux Topic
Search

 
TOURBUS
HOME PAGE
SAVVY
CONSUMER
FREE TECH
SUPPORT

LINUX PROGRAMMING

What Are Shell Script Variables?

A variable in a shell script is a means of referencing a numeric or character value. And unlike formal programming languages, a shell script doesn't require you to declare a type for your variables.
Thus, you could assign a number to the variable stuff and then use it again in the same script to hold a string of characters. To access the value (contents) of a variable, prefix it with a dollar sign.

stuff=5
stuff='chocolate truffles'

Don't put any spaces before or after the equal sign, or you'll get an error. And if you want to assign a string that contains spaces, you will need to put quotation marks around the string.

This is a good time to note that there are several distinct ways to use quotations marks in a shell script. Let's look at the differences among single quotation marks, double quotation marks, and the backslash character and then follow up with some examples.

· Single quotation marks, as in the preceding example, will always get you exactly what's inside the quotation marks--any characters that might otherwise have special meaning to the shell (like the dollar sign or the backslash) are treated literally.

· Use double quotation marks when you want to assign a string that contains special characters the shell should act on.

· The backslash is used to escape (treat literally) a single character (such as $ or *) that might otherwise be treated as a special character by the shell.

Now let's look at some examples that show when to use each method of quoting.

howdy='Good Morning $USER !'
echo $howdy

Good Morning $USER !

howdy="Good Morning $USER !"
echo $howdy
Good Morning hermie !

In the first case, the value of the howdy variable would probably not be what you wanted. The single quotation marks caused Bash to not treat $USER as a variable. In the second case, the results look much better. The double quotation marks allowed Bash to substitute the value of $USER, which is set automatically when you log in, in the string.

Here's another example that demonstrates a common error:

costmsg="Price is $5.00"
echo $costmsg
Actual result: Price is .00

We thought enough to quote the string, but the dollar sign tells Bash to use the value in the $5 variable, which is not what we wanted.. We can easily solve the problem by prefixing the dollar sign with a backslash, as shown here:

$ costmsg="Price is \$5.00"
$ echo $costmsg
Actual result: Price is $5.00

Arguments and Other Special Variables

Arguments are the values you pass to a shell script. Each value on the command line after the name of the script will be assigned to the special variables $1, $2, $3, and so on. The name of the currently running script is stored in the $0 variable.

Here are some other special variables you will find useful in script writing:

$# The number of arguments

$* The entire argument string

$? The return code from the last command issued

So let's try some examples working with arguments and other special variables. Create an executable script called testvars containing these lines:

echo "My name is $0"
echo "First arg is: $1"
echo "Second arg is: $2"
echo "I got a total of $# arguments."
echo "The full argument string was: $*"

Now if you run this script, here's what you'll see:

$ ./testvars birds have lips
My name is testvars
First arg is: birds
Second arg is: have
I got a total of 3 arguments.
The full argument string was: birds have lips

Previous Lesson: Executing a Script
Next Lesson: Shell Script Logic

[ RETURN TO INDEX ]




Ask Bob Rankin - Free Tech Support
<Send This Link to a Friend>         <Help>         <Bookmark This Page>


Copyright © by Bob Rankin
All rights reserved - Redistribution is allowed only with permission.