$*

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search

The value of all command-line arguments stored in $*, and it is known as a positional parameter. A list of all arguments to the script can be accessed using various ways.

Examples

Let us print all arguments passed to the script:

#!/bin/bash
for arg in $*
do
	echo "Command line arg: $arg"
done

Run it as follows:

./arg.sh One Two Three

Sample outputs:

Command line arg: One
Command line arg: Two
Command line arg: Three

$* vs "$*"

The values of $* and "$*" are different. When we put $* in double quotes ("$*") it expands to "$1n$2n$3n...", where n is the first character in the set of delimiters for the IFS variable. In other words, "$*" is one long string. Let us modify our script again

#!/bin/bash
## NOTE $* enclosed in "double quotes"
for arg in "$*"
do
	echo "Command line arg: $arg"
done

Run it as follows:

./arg.sh One Two Three

Sample outputs:

Command line arg: One Two Three