Command Line Arguments in linux shell program

Command Line Arguments in linux shell program

  • Shell scripts would not be very useful if we could not pass arguments to them on the command line
  • Shell script arguments are “numbered” from left to right

                 $1 – first argument after command

                 $2 – second argument after command

                 … up to $9

  • They are called “positional parameters”.

 

command-line-shell-variables
command-line-shell-variables

 

Example:

$ set how do you do
$ echo $1 $2
how do


Example 2

eduguru@localhost:~$ vi command_line_agruments.sh

#!/bin/bash
echo “There are $# arguments specified at the command line”.
echo “The arguments supplied are: $*”
echo “The first argument is: $1”
echo “The PID of the script is: $$”
eduguru@localhost:~$ chmod +x command_line_agruments.sh

eduguru@localhost:~$ ./command_line_agruments.sh Linux AIX HPUX VMware
There are 4 arguments specified at the command line.
The arguments supplied are: Linux AIX HPUX VMware
The first argument is: Linux
The PID of the script is: 16319