In a shell script, you can pass variables as arguments by entering arguments after the script name, for e.g. ./script.sh arg1 arg2. The shell automatically assigns each argument name to a variable. – Arguments are set of characters between spaces added after the script. To specify an argument that includes spaces, you need to enclose the complete argument in double quotation marks. Each script argument entered at the command line has a default variable name. The first argument after the script name is assigned to the variable $1, the second argument to $2, and so on.

 

This simple script displays information about a user.

#!/bin/bash
#The script demonstrates the use of variables entered at the command line.
echo “My name is $1”
echo “I work for $2”

Save the above script as name.sh and make it executable by using chmod command as below.

# chmod +x name.sh

Now run the script with two arguments.

OUTPUT

# ./name.sh foobar example.com

My name is foobar
I work for example.com

When you executed the above script, the first argument – foobar – was substituted for the $1 variable. Similarly, the second argument was substituted for the $2.

Range of command line arguments

Command-line arguments range from $0 to $9. Although the shell can access only ten variables simultaneously, you can program scripts to access an unlimited number of items that you specify at the command line. If you try to access the variable $10, the shell interprets $10 as referring to the $1 variable with a following 0 character. To access the value of the $10 variable, you use the shift command. This removes the first parameter’s value from the list, and replaces it with the second. The third value then replaces the second, and so on. The value originally at $10 then becomes the value of the $9 variable.

For example, this script – named var.sh – displays the values of the $1 to $10 variables. The shift command deletes the value of the first variable, and the value of each other variable shifts down and becomes the value of the preceding variable.

#!/bin/bash
echo “$1 $2 $3 $4 $5 $6 $7 $8 $9 $10”
shift
echo “$1 $2 $3 $4 $5 $6 $7 $8 $9 $10”

OUTPUT

# ./var.sh a b c d e f g h i j
a b c d e f g h i a0
b c d e f g h i j b0

Special Variable References

$#

The $# variable contains the total number of parameters entered on the command line. The value of this variable is always an integer. Consider the example of name.sh there were two parameters passed as Sandeep and example.com. So echo $# will output 2.

$*

This special variable contains all the command-line arguments that have been passed to a script, listed as a single word.

echo $* will output
foobar example.com

$@

The $@ special variable is similar to $* in that it contains all the values specified as command-line arguments. However, $@ lists each argument as a separate word, whereas $* does not distinguish between ordinary spaces separating values and those within quotation marks. The value Hello World is returned as “Hello World” using $*, whereas the same value is returned as “Hello” “World” when $@ is used.

echo $@ will output
foobar example.com

More Shell Variables:

$0

The variable $0 contains the script name itself.

$?

Echo’s the Exit Status of last command. The result is 0 (zero) if the command ran successfully and non-zero if it did not.

# echo $?
0

$$

Echo PID of current Shell

# echo $$
12354