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
Related articles
- Perl - Downloading a URL with a script - [22nd Mar 2006]
- Perl - Pattern Matching (Regular Expressions) - [12th Jan 2006]
- Shell Scripting - Reading Variables From STDIN - [11th Jan 2006]
- Perl - Configuring and Starting the mod_perl Server - [06th Jan 2006]
- Perl - Installing mod_perl in Three Steps - [06th Jan 2006]
- Perl - mod_perl - [06th Jan 2006]
- Perl - Associative Arrays - [06th Jan 2006]
- Perl - Manipulating Arrays - [06th Jan 2006]
- Perl - Arrays - [06th Jan 2006]
- Perl - Operators - [06th Jan 2006]
- Perl - Scalar Variables - [06th Jan 2006]
- Shell Scripting - Introduction - [08th Dec 2005]
- Shell Scripting - Invoking Shell Scripts - [08th Dec 2005]
- Shell Scripting - Variables - [08th Dec 2005]
- Shell Scripting - Referencing Variables - [08th Dec 2005]
- Shell Scripting - Command Arguments - [08th Dec 2005]
- Shell Scripting - Language Constructs - [08th Dec 2005]
- Perl - Introduction - [06th Dec 2005]
- MySQL - Connecting to the server - [06th Dec 2005]
- MySQL - Introduction - [06th Dec 2005]







