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 “”, 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.

[email protected]:~$ cat


Assign Executable permissions to the Script
[email protected]:~$ chmod +x
Now execute the scripts with command line arguments
[email protected]:~$ ./ 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. “” script below uses the shift command:

[email protected]:~$ cat


Now Execute the Script again.
[email protected]:~$ ./ 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
[email protected]:~$

Multiple shifts in a single attempt may be performed by furnishing the desired number of shifts to the shift command as an argument.

5 thoughts on “Command Line Arguments in Linux Shell Scripting”

  1. You missed [email protected], which is what a competent script would use (specifically “[email protected]”) to iterate over the input params, since either “$*” or $* would mangle parameters with shell metacharacters (i.e. would not work any time that user needs to quote the input parameters).

      1. If Command was called with eg. `Command “arg1” “arg two” “arg3″`, that’s three arguments.

        Saying `”$*”` in Bash means “a string with all the arguments joined by space. Saying `”[email protected]”`, means “an array with each argument”. (Kinda like spelling each argument but you don’t need to know how many there are.)

        Say you wanted to use first argument for some logic and pass rest of them to another command, let’s say it’s mysql. Now you could say, `foo=$1; shift` — that would “consume” the first argument (`arg1`) to `$foo`. Fine. Now your argument array only contains `arg two` and `arg3`.

        So now you want to pass these two arguments to, eg., “mysql”. But calling `mysql “$*”` just means calling mysql with *one* argument, that is, string `arg two arg3` which is something that basically never ever makes sense!.

        Calling `mysql “[email protected]”` is almost always what you want.

        Using non-quoted version of either syntax is even worse and pretty much useless: it just means that now the resulting value is subject to whole host of various bash expansions, word splitting (it’s `”argument” “two” “arg3”), etc. and may lead to unpredictable results based on eg. what is currently in the directory where the command is called.

Leave a Reply to Blaine Cancel reply

Your email address will not be published. Required fields are marked *