leestretton.com

You are in: > Home > Developers Guides

Shell Scripting - Referencing Variables

The value of a shell variable can be referenced by placing a $ before the name of the variable. The command

echo $path

will output the value of the variable $path. Or you can access the variable by enclosing the variable name in curly brace characters, and then prefixing it with a $. The command

echo ${path}

would have the same result as the last example. The second method is used when something is to be appended to the contents of the variable. For example, consider the commands

set fname = prog1
rm ${fname}.c

These would delete the file `prog1.c'.

To see how many elements are in a variable's list, we prefix with a # then a $. The command

echo $#V

above would print 3 to the screen, while

echo $#path

would reveal the number of directories in your search path.

The @ command can be used for computations. For example, if you have shell variables $X and $Y, you can set a third variable $Z to their sum by

@Z = $X + $Y

Problem with this page? Let us know
© Lee Stretton 2005 | powered by linux powered by php 5 powered by MySQL download rss feeds powered by php 5 valid css