user2601569
user2601569

Reputation: 1

Value of variable $*

I'm a newbie Linux user, and I have run into this problem.

If the following commands have been executed

set 10 20 30 40 50
shift
shift 

how would I find the value of $*?

Upvotes: 0

Views: 5186

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 754520

The simplest way to find the value of $* is to echo it:

echo $*

Given the three steps shown, it will echo:

30 40 50

The value of $* without quotes is the same as the value of $@ without quotes, and consists of all the positional arguments to the shell script, or the values set by a set command such as the one you showed (as modified by any subsequent shift commands). With double quotes around them, "$*" represents a single string with all the positional parameters in it, but "$@" represents a number of strings, each one identical to a positional parameter. It is the form that is more commonly correct.

You can see the difference between these forms with the printf command:

printf "%s\n"  $*
printf "%s\n" "$*"
printf "%s\n"  $@
printf "%s\n" "$@"

You'd get more insight into them if you used:

set -- 10 '20 30' '  40  50  '

without any shifts, and then used the 4 printf commands:

printf "X%sX\n"  $*
printf "X%sX\n" "$*"
printf "X%sX\n"  $@
printf "X%sX\n" "$@"

The X's serve to mark the start and end of the values printed. The output is:

X10X
X20X
X30X
X40X
X50X
X10 20 30   40  50  X
X10X
X20X
X30X
X40X
X50X
X10X
X20 30X
X  40  50  X

The relevant section of the Bash manual is special parameters.

Upvotes: 2

Nitin4873
Nitin4873

Reputation: 17562

its a bit odd ..... the question says , value of $* ...... well that stands for everything that was put into the command and hence was passed onto the posistional parameters .

the shift command will just push everything that was there in the original command line one place left .... so from 10 20 30 40 50 ..... after two sahifts (2 times push to the left) you have 30 40 50 now that can be accessed by specifying their position/occurence with a $ like $1 ... $2

$ set 10 20 30 40 50
$ shift 2
$ echo $*
30 40 50

to put it simply ....

if you want to access individual values .... just use $1 , $2 .... like wise , here $1 will have 30 and $2 will have 40.

of you want to know how many parameters you have use $#

$ echo $#
 3

does this help ?

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 225042

From the bash man page:

set: Any arguments remaining after the options are processed are treated as values for the positional parameters and are assigned, in order, to $1, $2, ... $n.

And:

shift [n]: The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset... If n is not given, it is assumed to be 1.

So your set sets:

$1 = 10
$2 = 20
$3 = 30
$4 = 40
$5 = 50

And then the shift commands throw out $1 and shuffle the other $n variables down by one. You do it twice, so that leaves you with:

$1 = 30
$2 = 40
$3 = 50

You could just use: echo $* to see the output:

30 40 50

Upvotes: 0

Related Questions