How to Pass Command Line Arguments to Bash Script

In this tutorial, we will learn how to pass command line arguments to a bash shell script in Linux.

Command line arguments 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.

command-line-arguments

command-line-shell-variables

Let’s create a shell script with name “arguments.sh”, it will show the command line arguments that were supplied and count number of arguments, value of first argument and Process ID (PID) of the Script.

$ vi arguments.sh
#!/bin/bash
#This Script demonstrate the usage of command line arguments in bash script

echo "There are $# arguments pass at command line"
echo "The arguments supplied are : $*"
echo "The first command line argument is: $1"
echo "The PID of the script is: $$"

Save and close the  file.

Assign Executable permissions  to the script using chmod command

$ chmod +x arguments.sh

Now execute the script with following command line arguments

$ ./arguments.sh Debian RockyLinux Ubuntu RHEL SUSE

output,

Pass-command-line-arguments-bash-script

In above script, we can also use ‘$@’ in place of $* to get all the arguments. The key difference between these two variables is that ‘$*’ represent all the parameters in single string whereas ‘$@’ represent arguments as a array or $@ expands to multiple arguments.

‘$@’ is always preferred over ‘$*’, to understand their difference , let’s create following script,

$ vi arguments-new.sh
#!/bin/bash
echo "With *:"
for arg in "$*"
do
echo "<$arg>"
done
echo
echo "With @:"
for arg in "$@"
do
echo "<$arg>"
done

Save & exit the file.

Execute the script and notice the difference

Arguments-Example-Bash-Script

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. The above script would look like below after adding shift command to it.

$ cat arguments.sh
#!/bin/bash
#This Script demonstrate the usage of command line arguments in bash script

echo "There are $# arguments pass at command line"
echo "The arguments supplied are : $*"
echo "The first command line argument is: $1"
echo "The PID of the script is: $$"
shift
echo "New first argument after first shift: $1"
shift
echo "New first argument after second shift: $1"

Let’s re-run the script with following command line arguments,

$ ./arguments.sh Windows Linux MacOS HPUX
There are 4 arguments pass at command line
The arguments supplied are : Windows Linux MacOS HPUX
The first command line argument is: Windows
The PID of the script is: 2193
New first argument after first shift: Linux
New first argument after second shift: MacOS
$

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

Note: Command line arguments are also known as positional parameters.

That’s all from this tutorial, I hope you have understood how to pass command line arguments to a bash script.

Also Read: How to Use Conditional Statements in Bash Script

5 thoughts on “How to Pass Command Line Arguments to Bash Script”

  1. You missed $@, which is what a competent script would use (specifically “$@”) 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).

    Reply
      • 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 `”$@”`, 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 “$@”` 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.

        Reply

Leave a Comment