|
CSC128: Introduction to UNIX
Variables and Arguments
Bourne Shell Variables
Information
can be stored in shell variables (also known as parameters).
Each variable must have a name, which can consist of letters, digits,
and underscores. Note, however, that a variable cannot start with
a digit. To assign a value to a variable, use the = symbol.
user@host $ myvar=abc
This will assign the value
abc to the variable named myvar . This value will be available in the
current shell only. This variable will exist for any subshells (shells
executed from a shell). To make the value available to subshells, use the
export command.
user@host:~$ export myvar
It is customary to use ALL
CAPITAL LETTERS for variables that are exported.
To display the value of a
variable, use the echo command:
user@host:~$ echo $myvar
abc
To reference a variable,
it must be prefixed with a dollar sign ($ ). Otherwise, echo would simply display the name of
the variable and not expand it to its value.
In some situations, the
shell may confuse the name of a variable with text that immediately follows.
Use braces ({} ) to surround the variable name if
this is likely.
user@host:~$ myvar=efgh
user@host:~$ echo abcd$myvarijkl
abcd
user@host:~$ echo abcd${myvar}ijkl
abcdefghijkl
To see a list of all
the variables in the current shell's environment, use the command
set with no arguments.
Quotations
When setting
variables, bash will stop at whitespace. For example:
user@host:~$ myvar=now
user@host:~$ myvar=now is the time
user@host:~$ echo $myvar
now
Both of these commands
will set the value of myvar to now . To assign the entire line,
including spaces, surround the text with quotes:
user@host:~$ myvar='now is the time'
-or-
user@host:~$ myvar="now is the time"
There is a difference
between single and double quotes. Values in single quotes will be
taken verbatim. Values in double quotes will be expanded before
they are used. Examples:
user@host:~$ drink=cola
user@host:~$ order='I want a glass of $drink'
user@host:~$ echo $order
I want a glass of $drink
user@host:~$ order="I want a glass of $drink"
user@host:~$ echo $order
I want a glass of cola
Removing Variables
To remove the
value assigned to a variable, set it to nothing.
user@host:~$ myvar=
To remove
the variable entirely, use unset :
user@host:~$ unset myvar
Keyword Shell Variables
Many variables
have special meaning to the shell and may change the shell's behavior. Here
are a few:
HOME defines the user's home directory.
If you change it, the meaning of the tilde (~ ) will change to mean the directory
HOME is now defined as.
PATH holds a list of directories,
seperated by colons (: ). When you type a command (that
is not a builtin) into the shell, it searches these directories in
order for an executable file that matches the name of the command. If
it finds it, it executes it. Note that the shell does not search
the current working directory unless it is in the PATH .
PROMPT, PS1, PS2, etc. These variables hold information about
what the shell prompt should look like and what information it should give.
See man bash for more information.
Shell Script Arguments
or Positional Parameters
There are several
shell variables that are particularly useful in scripts.
$0 This will hold the name of the script
as it was called from the command line.
$1, $2, $3, ... $9, ${10}, ${11} etc.
These will hold the arguments passed
from the command line. Each sequence of characters separated by whitespace
will be placed in a different variable. Quotes will force a sequence
with spaces into a single variable.
$*
This holds all the arguments passed
from the command line as a single long string of characters.
$@
Like $* this holds all the arguments passed
from the command line but it keeps them separated.
$#
This will return the number of arguments
passed from the command line.
$?
This will return the exit value or
exit status
of the last program run. When a program exits normally, it should
return an exit value (held in the variable '$?') of 0. If there was an error, it should
return a non-zero exit value, sometimes indicating the nature of the
error.
The arguments can be changed with the
set
command.
user@host:~$ set one two three
user@host:~$ echo $#
3
user@host:~$ echo $0
bash
user@host:~$ echo $1
one
user@host:~$ echo $2
two
user@host:~$ echo $3
three
The arguments can be shifted down
one space with the shift command. Note that $0 remains the same.
user@host:~$ shift
user@host:~$ echo $0
bash
user@host:~$ echo $1
two
user@host:~$ echo $2
three
user@host:~$7 echo $3
Toolbox
History Mechanism (textbook, pp. 340-344)
aliases (textbook, pp. 348-350)
|