Command Line Arguments in Shell Script 

Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part.

Positional Parameters

Command-line arguments are passed in the positional way i.e. in the same way how they are given in the program execution. 


let's see it with an example


#!/bin/bash

name=${1}
age=${2}

echo "Hello my name is ${name} and my age is ${age}"


now let's give execute permission to this program and execute it.

┌──(gaurav㉿learning-ocean)-[~/shellscript-youtube]
└─$ chmod +x commandlineargs.sh

┌──(gaurav㉿learning-ocean)-[~/shellscript-youtube]
└─$ ./commandlineargs.sh gaurav 30
my name is gaurav, and my age is 30


Suggestion : please use ${1} instead of $1.


#!/bin/bash
name=${1}
age=${2}
echo ${1}
echo ${2}
echo ${3}
echo ${4}
echo ${5}
echo ${6}
echo ${7}
echo ${8}
echo ${9}
echo ${11}
echo ${12}
echo ${13}
echo "my name is ${name}, and my age is ${age}"
echo $#
echo [email protected]
echo $*



Special Variables:


${0} will represent the shell script name itself.

${#} or $# will show us the Total number of arguments and it is a good approach for loop concepts.


$* or ${*} In order to get all the arguments as double-quoted, we can follow this way


[email protected] or ${@} Values of the arguments that are passed in the program. This will be much helpful if we are not sure about the number of arguments that got passed.