next up previous
Next: Command Line Parameters Up: The Shell Script Previous: The Basics

Variables

Shell variables work on the principle of 'variable substitution'. This is different from nonscripting languages such as c$+$$+$. If you want to use the name of a variable, type the name itself. If you want to substitute its value, type the dollar sign $ followed by name. In case the value of the variable contains spaces, we have to use pair of double quotes (see the example below). To delimit name of the variable one has to use curly braces {}, as shown in the example.

Example:
#! /bin/sh
class=cs241
longname="I like cs241"
echo class #prints class
echo $class #prints cs241
echo $classroom #prints nothing
echo ${class}room #prints cs241room
echo $longname #prints I like cs241

After execution the output will be:
class
cs241
cs241room
I like cs241



Instructional Support Group 2008-08-05