Positional parameters are used to pass arguments to a command line application. This post shows you how to used positional parameter in bash.

On your terminal, type the following command:

cal -y 2012

This command would print the calendar for 2012. -y and 2012 are positional paramters. Positional parameters are used to pass arguments to the program. The arguments become available to program as numbered parameters. In the above command, the arguments are passed as follows:

$0 = cal
$1 = -y
$2 = 2012

Following example prints the positional parameters:

#!/bin/bash
#: Title       : using positional parameter
#: Description : show how to print and use positional parameters
#: Options     : war and year
#: Example     : $ ./upp.sh WWII 1445

printf "Filename: %s\n" "$0"
printf "First Argument: %s\n" "$1"
printf "Second Argument: %s\n" "$2"
printf "%s ended in %s\n" "$1" "$2"

Type the following command to run the code:

$ ./upp.sh WWII 1945

Output:

Filename: upp.sh
First Argument: WWII
Second Argument: 1945
WWII ended in 1945

By master