Friday, 18 June 2021

Shell script to work with command line arguments

In this shell script, we will learn the use of the commands to display the first command-line argument and the total number of command-line arguments in a shell script.

$0 - always refers to the shell program being executed

$# - returns the total number of arguments given in the command line

$@ - returns the entire list of the arguments in the command line

$* - returns the entire list of arguments in the command line double quoted

$1, $2,.............., $9 -  returns the arguments in the position specified


PROGRAM

#Shell script to work with command line arguments
echo -e "\nThe first argument in a command line refers to the program name"
echo -e "The first argument here is $0 \n"
echo "The list of arguments given in the command line are"
echo -e "$* \n"
echo "The total number of command line arguments is " : $#



OUTPUT

$ sh clarg.sh hello welcome to unix programming


The first argument in a command line refers to the program name

The first argument here is clarg.sh


The list of arguments given in the command line are

hello welcome to unix programming


The total number of command line arguments is  : 5