Submitted by: Submitted by tystud
Views: 448
Words: 1968
Pages: 8
Category: Other Topics
Date Submitted: 02/20/2011 09:19 AM
Unix Lab Exercise 6
Introduction to Unix - POS420
Parameter Variables
These are some of the variables you have to know. All these variables having a prefix $ are called parameter variables.
These are some of the variables we have to know.
$# number of arguments on the command line
$0 the name of the current shell or program
$1 the first argument
$2 the second argument
..
..
$9 the ninth argument
$* all arguments on the command line ("$1 $2 ... $9")
$@ all arguments on the command line, each separately quoted ($1 $2 ..)
$ cat script3.sh
if [ $# -lt 3 ]
then
echo "Please enter at least three names"
exit
fi
echo "You entered $# names"
echo "The first name is $1"
echo "The second name is $2"
echo "The third name is $3"
echo "this script name is $0"
echo "You have entered these names: $*"
echo "You have entered these names: $@"
Let us run
$ chmod +x script3.sh
$ ./script3.sh tom
Please enter at least three names
$ script3.sh tom smith kay scott
You entered 4 names
The first name is tom
The second name is smith
The third name is kay
This script name is script3.sh
You have entered these names: tom smith kay scott
You have entered these names: tom smith kay scott
Let us run one more time with quotes for a name since there is a space in one name
$ script3.sh kay ron 'tom smith'
You entered 3 names
The first name is kay
The second name is ron
The third name is tom smith
this script name is script3.sh
You have entered these names: kay ron tom smith
You have entered these names: kay ron tom smith
Another example :Enter the following shell script in vi editor.
$ cat variables.sh
echo "$#:" $#
echo '$#:' $#
echo '$-:' $-
echo '$?:' $?
echo '$$:' $$
echo '$!:' $!
echo '$3:' $3
echo '$0:' $0
echo '$*:' $*
echo '$@:' $@
When executed with some arguments it displays the values for the shell variables, e.g.:
$ ./variables.sh one two three...