Positional Parameters
Linux Commands - Shells

Positional Parameters

Special built-in environmental  variables are positional parameters which hold command-line arguments to positions with the names 1,2,3,4, etc. which are indicated by $1,$2,$3,$4, etc.  Argument $0 is the name of the script.


Lesson 7 | Lesson 9

Some variables are set for the user already.  These variables can be used by a script.  These variables are $0-$9 and $#.


#!/bin/sh
# Understanding Parameters
#
echo "Call with $# parameters"
echo "The Shell Script Name is $0"
echo "The first parameter is $1"
echo "The second parameter is $2"
echo "The third parameter is $3"
echo "All parameters are $@"


./var.sh
Call with 0 parameters
The Shell Script Name is ./var.sh
The first parameter is
The second parameter is
The third parameter is
All parameters are


Now when you execute the script include three parameters fred tom harry.  Notice these are listed right after the script in the output.  This illustrates that the first nine variables are automatically picked up and read by the system.

./var.sh fred tom harry
Call with 3 parameters
The Shell Script Name is ./var.sh
The first parameter is fred
The second parameter is tom
The third parameter is harry
All parameters are fred tom harry

./var.sh fred tom harry
Call with 3 parameters
The Shell Script Name is ./var.sh
The first parameter is fred
The second parameter is tom
The third parameter is harry
All parameters are fred tom harry

 


Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874