What are different types of positional parameters available in linux ?

Positional parameters as its name suggests are different positional variables automatically defined by linux shell.

The shell automatically assigns the passed parameters according to their position in 9 different parameters(1 to 9), known as positional parameters.

These positional parameters are used whenever we want to convey information to the program and to use it inside the program.

There are total 9 positional parameters available i.e. from $1 to $9, we can define them by specifying arguments at the command line after the program.

$1 to $9 are 9 positional parameters and $0 parameter is also defined by shell, due to which sometimes they are referred as 10 variables.

$0 -> Name of shell program or script.
$1 -> First argument passed.
$2 -> Second argument passed.
$3 -> Third argument,
..
..
$9 -> 9th argument passed.

 

Positional Parameters Example

### Lets create a script file like below.
ngelinux: saket$ cat test1.sh
#!/bin/bash
 echo "$0 script executed with below positional parameters..."
 echo "First: $1"
 echo "Second: $2"
 echo "Third: $3"
 echo "Fourth: $4"
 echo "Fifth: $5"
 echo "Sixth: $6"
 echo "Seventh: $7"
 echo "Eighth: $8"
 echo "Ninth: $9"
ngelinux: saket$

### Lets run it like "script-name parameters"
ngelinux: saket$ ./test1.sh Welcome to ngelinux .com this is positional parameter tutorial wow
./test1.sh script executed with below positional parameters...
First: Welcome
Second: to
Third: ngelinux
Fourth: .com
Fifth: this
Sixth: is
Seventh: positional
Eighth: parameter
Ninth: tutorial

As we can see above, different parameters passed to script are assigned to different positioned parameters above and can be used in the shell script.

Leave a Reply

avatar
  Subscribe  
Notify of