|
Page 1 of 2 Special Variable Types in Shell
Some of the Special variable types in Shell programming
local variables
variables visible only within a code block
or function (see also local variables in functions) environmental
variables
variables that affect the behavior of the
shell and user interface
Note
In a more general context, each process
has an "environment", that is, a group of variables that hold
information that the process may reference. In this sense, the
shell behaves like any other process.
Every time a shell starts, it creates
shell variables that correspond to its own environmental variables.
Updating or adding new environmental variables causes the shell to
update its environment, and all the shell's child processes (the
commands it executes) inherit this environment.
Caution
The space allotted to the environment is
limited. Creating too many environmental variables or ones that use
up excessive space may cause problems.
bash$ eval "`seq 10000 | sed -e
's/.*/export var&=ZZZZZZZZZZZZZZ/'`"
bash$ du
bash: /usr/bin/du: Argument list too
long
Note: this "error" has been fixed, as of
kernel version 2.6.23.
(Thank you, Stéphane Chazelas for
the clarification, and for providing the above example.)
If a script sets environmental variables,
they need to be "exported", that is, reported to the environment
local to the script. This is the function of the export
command.
Note
A script can export variables only to
child processes, that is, only to commands or processes which that
particular script initiates. A script invoked from the command line
cannot export variables back to the command line environment. Child
processes cannot export variables back to the parent processes that
spawned them.
Definition: A child process is a
subprocess launched by another process, its parent.
positional parameters
arguments passed to the script from the
command line: $0, $1, $2, $3 . . .
$0 is the name of the script itself, $1 is
the first argument, $2 the second, $3 the third, and so forth. [1]
After $9, the arguments must be enclosed in brackets, for example,
${10}, ${11}, ${12}.
The special variables $* and $@ denote all
the positional parameters.
Example: Positional Parameters
#!/bin/bash
# Call this script with at least 10
parameters, for example
# ./scriptname 1 2 3 4 5 6 7 8 9 10
MINPARAMS=10
echo
echo "The name of this script is
"$0"."
# Adds ./ for current directory
echo "The name of this script is
"`basename $0`"."
# Strips out path name info (see
'basename')
echo
if [ -n "$1" ] # Tested variable is
quoted.
then
echo "Parameter #1 is $1" # Need quotes to
escape #
fi
if [ -n "$2" ]
then
echo "Parameter #2 is $2"
fi
if [ -n "$3" ]
then
echo "Parameter #3 is $3"
fi
# ...
if [ -n "${10}" ] # Parameters > $9
must be enclosed in {brackets}.
then
echo "Parameter #10 is ${10}"
fi
echo
"-----------------------------------"
echo "All the command-line parameters are:
"$*""
if [ $# -lt "$MINPARAMS" ]
then
echo
echo "This script needs at least
$MINPARAMS command-line arguments!"
fi
echo
exit 0
Bracket notation for positional parameters
leads to a fairly simple way of referencing the last argument
passed to a script on the command line. This also requires indirect
referencing.
args=$# # Number of args passed.
lastarg=${!args}
# Note: This is an *indirect reference* to
$args ...
# Or: lastarg=${!#} (Thanks, Chris
Monson.)
# This is an *indirect reference* to the
$# variable.
# Note that lastarg=${!$#} doesn't
work.
|