Monthly Archives: October 2016

Passing arguments to shell script

We can pass the parameters (arguments) to shell script delimited by white spaces. You can access the current script name and arguments using $0, $1,$2….and so on.

$0 = Script name (also called as base name of file)

$1 = First argument to the script

$2 = Second argument to the script and so on..

In the script, we can access count of arguments using $#

if [ $# < 2 ]
then
          echo “Count of parameters is less than 2”
exit -1
fi

We can use below special variables to access the parameters.

  1. $@ – All of the arguments.
  2. $* – All of the arguments. This does not preserve the white space and quotes.

Another variables that start with $ are given below.

  1. $$ – This gives you the process id of current shell.
  2. $! – This gives you the process id of last background process.