Menu

Command Line Arguments in Linux Shell Scripting

Overview :


Command line arguments (also known as positional parameters) are the arguments specified at the command prompt with a command or script to be executed. The locations at the command prompt of the arguments as well as the location of the command, or the script itself, are stored in corresponding variables. These variables are special shell variables. Below picture will help you understand them.


Let’s create a shell script with name “command_line_agruments.sh”, it will show the command line argruments that were supplied and count number of agruments, value of first argument and Process ID (PID) of the Script.
hackthesec@localhost:~$ cat command_line_agruments.sh

Assign Executable permissions to the Script
hackthesec@localhost:~$ chmod +x command_line_agruments.sh
Now execute the scripts with command line argruments
hackthesec@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: 16316
Shifting Command Line Arguments
The shift command is used to move command line arguments one position to the left. During this move, the first argument is lost. “command_line_agruments.sh” script below uses the shift command:
hackthesec@localhost:~$ cat command_line_agruments.sh

Now Execute the Script again.
hackthesec@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 Process ID of the script is: 16369
The new first argument after the first shift is: AIX
The new first argument after the second shift is: HPUX
hackthesec@localhost:~$
Multiple shifts in a single attempt may be performed by furnishing the desired number of shifts to the shift command as an argument.

www.hackthesec.co.in
www.twitter.com/hackthesecurity

About Author:


I am a Linux Administrator and Security Expert with this site i can help lot's of people about linux knowladge and as per security expert i also intersted about hacking related news.TwitterFacebook

Next
Newer Post
Previous
Older Post

0 comments:

Post a Comment

 
Top