|
PARAMETERS
AND VARIABLES A
shell parameter is associated with a value that is accessible to the user. Names
for variables (user created): Myvar
(start with characters only) __MyVar__ (ok ) 45myvar
(can not have variable that start with numbers) Environmental
variables, variables that have special meaning to shell: HOME,
PATH
(variables that are called environmental variables) export
varname This
will make the variable to be accessible to other shell and other programs. echo - copies the arguments of a variable to the standard output I.E. $person=alex
^ ^ No
spaces
(give to the variable person the value of
alex.) $echo
person
(it will just echo the word person) $echo
$person
(will display the contents of the variable myvar. Because of the leading
$, shell recognizes that $person is the name of a variable, substitutes the
value of the variable, and passes that value to echo) $echo
"$person"
(the " " turn off the special meaning of most other characters,
also allows assignment of strings with spaces
i.e. person="bob jones") $echo '$person' $echo
\$person
(prevents variable substitution, you will get $person) $memo=test* $echo "$memo*"
(memo*) $echo
$memo (test1 test2
.....) /etc/profile (it has the
system wide default values) .bash_profile (if you need to
change the value of one of the system wide default values)
Removing
values of variables $person=
(removes the value of the variable, but not the variable itself) or $unset
person The
readonly Builtin $person=jenny $echo
$person (jenny) $readonly
person $person=hellen
(person: read-only variable) $readonly
person=jenny
(you can also use the readonly at initialization) Variables
are local to the process in which they are declared. Usually a shell script does
not have access to those variables unless you export them. $cat
extestl cheese=american
$ cat
subtest echo
"subtest 1: $cheese" $ extestl
extestl
1: american $ cat
extest2 export
cheese $ extest2
extest2
1: american The
declare Builtin The
declare builtin allows you to set attributes and values for shell variables. -f
option makes a variable a function name -i
option marks a variable so that integer variables are stores efficiently -r
option makes a variable readonly -f
option marks a variable for export. $ declare
personl=alex $ declare
-r person2=jenny $ declare
-rx person3=helen = $ declare -x -r
person3=helen $ declare
-x person4 $declare
+r person3
(use + to remove and attribute) The
read Builtin $cat
read1 echo
-n "Go ahead: " $read1 Go
ahead: this is a line You
entered: This is a line echo
"Go ahead: \c" =
echo -n "Go ahead: "
(if your shell does not undersnda -n) Command
substitution Command
substitution replaces a command with an output of a command $echo
$(pwd)
(newer bash syntax) /home/alex $echo
`pwd`
(it works on older and newer) /home/alex $cat
where where=$(pwd)
(can also use `pwd`) echo
"You are using the $where directory." $where You
are using the /home/jenny directory. Keyword
variables Bash uses certain
shell variables in the same way as the Bourne shell. In some cases, Bash assigns
a default value to the variable.
A colon-separated list of directories used as a
search path for the
The current user's home directory; the default for
the
A list of characters that separate fields; used when
the shell splits words as part of expansion.
If this parameter is set to a filename and the
A colon-separated list of filenames which the shell
periodically checks for new mail. Each list entry can specify the message that
is printed when new mail arrives in the mail file by separating the file name
from the message with a `?'. When used in the text of the message,
A colon-separated list of directories in which the
shell looks for commands.
The primary prompt string. $ PS1="\h: " (hostname of
your machine) see page 331 for list bravo: echo test tset bravo:
The secondary prompt string. The default value is `>
'. $echo "second prompt you will see the secondary prompt > type " and it will finish Positional
Parameters When
you call a shell script, positional parameters are the command name and
arguments. They are called positional because you refer to them by their
position on the command line. $0
(stores the name of the command you used to call a program) $ cat
abc echo
The name of the command used echo to execute this shell script was $0 $ abc The
name of the command used to
execute this shell script was abc $ cat
display_5args echo
The first five command line echo
arguments are $1 $2 $3 $4 $5 $ display_5args
jenny alex helen The
first five command line arguments
are jenny alex Helen The
shift Builtin The
shift builtin promotes each of the command-line arguments. The second argument
(which was represented by $2) becomes the first (now represented by $1), the
third becomes the second, the fourth becomes the third, and so on. $
cat demo_shift echo "argl= $1
arg2= $2
arg3= $3" shift echo
"argl= $1
arg2= $2
arg3= $3" shift echo
"argl= $1
arg2= $2
arg3= $3" shift echo
"argl= $1
arg2= $2
arg3= $3" shift $
demo_shift alice helen jenny argl=
alice
arg2= helen arg3=
jenny argl=
helen
arg2= jenny arg3= argl=
jenny
arg2= arg3= argl=
arg2= arg3= shift:
shift count must be <= $#
(count of the command line arguments) The
set Builtin When
you call the set builtin with one or more arguments, it uses the arguments as
values for positional parameters, starting with $1. The following script uses
set to assign values to the positional parameters $1, $2, and $3: See
page 336 for additional options with the set command $
cat set_it set
this is it echo
$3 $2 $1 $
set_it it
is this $
date Sun
Dec 8 14:07:38 PST 1996 $
cat dateset set
$(date) echo
$*
(All of the command line arguments) echo echo "Argument 1: $1" echo "Argument 2: $2" echo "Argument 3: $3" echo "Argument 4: $4" echo echo "$2 $3, $6" $
dateset Sun
Dec 8 14:07:42 PSI 1996 Argument
1: Sun Argument
2: Dec Argument
3: 8 Argument
4: 14:07:42 Dec
8, 1996 Special
parametes $n Value of the
nth command-line argument (can be changed by set) $* All of the command-line arguments (can be changed
by set) $@ All of the command-line arguments (can be changed by set) $# Count of the
command-line arguments $$ PID number of the current process $! PID number of the most recent
background task $? Exit status of the last task that was
executed (a nonzero exit status represents a false values and means that the
command failed
|
Back to
CS140U Homepage |