Shell Programming - Command Line Arguments

Unix

Share

In this tutorial we will learn how to handle command line arguments in Shell Programming.

We use command line arguments to provide input to shell script.

Syntax

$ sh filename.sh argument1 argument2 ... argumentN

Where, filename.sh is a shell script file and argument1, argument2 ... argumentN are list of arguments.

Example:

$ sh greetings.sh Hello World

In the above example, greetings.sh is the script file. Hello and World are the arguments.

Special variables

There are some special variables that we get to handle command line arguments in shell programming.

The $0 variable

This holds the name of the script.

The $1 $2 ... $N variables

These variables hold the arguments provided to the script.

The $# variable

This variable hold the total number of arguments passed to the script.

The $@ and $* variables

They both holds the list of arguments provided to the script.

Example #1: Write a Shell Script to take user data as command line argument and display a greetings message

#!/bin/sh

# display the file name
echo "The name of the script file is $0"

# display total number of arguments passed to the script
echo "Total number of arguments passed to the script = $#"

# display all the arguments using for loop
if [ $# -gt 0 ]
then
  
  echo "List of arguments:"
  for arg in $@
  do
    echo "$arg"
  done

else
  
  echo "No argument provided to the script."

fi

Output:

$ sh example01.sh 
The name of the script file is example01.sh
Total number of arguments passed to the script = 0
No argument provided to the script.


$ sh example01.sh My name is Yusuf Shakeel
The name of the script file is example01.sh
Total number of arguments passed to the script = 5
List of arguments:
My
name
is
Yusuf
Shakeel

Explanation:

In the above script we are using the $0 variable to get the name of the script file i.e., example01.sh.

We are using the $# command to get the total number of arguments passed to the script file.

We are using if statement to check if the user provided any argument to the script. If $# is 0 then we echo No argument provided to the script.

If the user provides 1 or more number of arguments to the script file then $# is greater than 0 and so, we use the for loop to pick each argument stored in variable $@ and assign it to variable arg.

Then, inside the body of the for loop we echo each of the arguments.

The $? variable

This variable holds the exit value of the last run command or return code from a function.

So, for example if we call a function that is supposed to return any value back then we can get that returned value using the $? variable.

Example #2: Write a Shell Script to display some result based on return value from a function call

In the following example we are checking if argument was provided based on return code from a function call.

#!/bin/sh

# function to check number of arguments passed to the script
function isArgumentPresent {
  if [ $1 -gt 0 ]
  then
    return 0	# success code
  else
    return 1	# failure code
  fi
}

# calling the function
# and passing number of arguments passed to the script
isArgumentPresent $#

# get the returned code
returnedCode=$?

# check returnedCode
if [ $returnedCode -eq 0 ]
then
  echo "Arguments present!"
else
  echo "Arguments not present!"
fi

Output:

$ sh example02.sh hello world
Arguments present!

$ sh example02.sh 
Arguments not present!

The $$ variable

This variable holds the PID i.e., Process IDentifier of the currently running script.

One use case of PID is to append it when creating temporary files from the script.

Example: temp-file-$$.tmp

Example #3: Write a Shell Script to print the PID of the currently running script

#!/bin/sh

echo "PID of the current file is $$"

Output:

$ sh example03.sh 
PID of the current file is 71084

The $! variable

This variable holds the PID of the last run background process.

Example #4: Write a Shell Script to print the PID of the last run background process

#!/bin/sh

echo "The PID of the last run background process was $!"

Output:

$ sh example04.sh 
The PID of the last run background process was 84014
Share