This tutorial will show you an example on using arguments to a unix shell script or programming. Here we will create a shell script file for showing the example.
Knowledge of unix
Now open a unix terminal and create a shell script file called
name.sh. The shell script file extension is
.sh. Write the below content in name.sh file.
echo "My first name is $1" echo "My last name is $2" echo "Total number of arguments is $#"
In the above program you see how we are indicating command line arguments.
$2 are the command line arguments passed along with the file name from unix command. The third line shows the total count of command line arguments passed to the program using the syntax
Run the script
$ ./name.sh Soumitra Roy My first name is Soumitra My last name is Roy Total number of arguments is 2
We are assuming that the shell script file name.sh exists in the current directory. Here we are executing the shell script file from current directory using command ./name.sh and we have passed two arguments – Soumitra and Roy. And here in the output you see how the argument’s value is displayed.
So you have got an idea on using arguments to a unix shell script from the above example.
Thanks for reading.