what is difference between $@ and $* in UNIX Shell script

Answers were Sorted based on User's Feedback



what is difference between $@ and $* in UNIX Shell script..

Answer / yogesh malkoti

In a shell script :

$@: each quoated string treated as a separate argument
while gving at command line.

$*: stores the complete set of positional parameters as a
single string

Is This Answer Correct ?    45 Yes 3 No

what is difference between $@ and $* in UNIX Shell script..

Answer / rajkapooor

$@ all quated string in command line treated as seprate
argument
$* stores all argument as a single argument

Is This Answer Correct ?    12 Yes 2 No

what is difference between $@ and $* in UNIX Shell script..

Answer / alf55

There is a difference between using $@ and using "$@". The
first is the same as using $*, while the latter is what was
being described ad $@. It only handles the arguments
correctly when used as "$@". However, you will not see where
the arguments are changing in its simple usage in a print.

echo "arguments are:"; for arg in "$@"; do echo "
${arg}"; done

Will show each argument on a new line indented by four spaces.

Here is an example:
[code]
bash$ function show_simple_args
> {
> echo "There are $# arguments passed, can you find
them correctly?"
> echo "using \$*:"
> echo $*
> echo "using \$@:"
> echo $@
> echo "using \"\$@\":"
> echo "$@"
> echo "using for loop with \$*:"
> echo "arguments are:"; for arg in $*; do echo "
${arg}"; done
> echo "using for loop with \$@:"
> echo "arguments are:"; for arg in $@; do echo "
${arg}"; done
> echo "using for loop with \"\$@\":"
> echo "arguments are:"; for arg in "$@"; do echo "
${arg}"; done
> }
bash$
bash$ show_simple_args "arg 1" "arg 2" "arg 3" "arg 4"
There are 4 arguments passed, can you find them correctly?
using $*:
arg 1 arg 2 arg 3 arg 4
using $@:
arg 1 arg 2 arg 3 arg 4
using "$@":
arg 1 arg 2 arg 3 arg 4
using for loop with $*:
arguments are:
arg
1
arg
2
arg
3
arg
4
using for loop with $@:
arguments are:
arg
1
arg
2
arg
3
arg
4
using for loop with "$@":
arguments are:
arg 1
arg 2
arg 3
arg 4
bash$
[/code]

Is This Answer Correct ?    9 Yes 1 No

Post New Answer

More Linux Commands Interview Questions

How many commands are there in linux?

0 Answers  


you wish to create a link to the /data directory in bob's home directory so you issue the command ln /data /home/bob/datalink but the command fails. What options hould you use in this command line to be successful.

0 Answers  


What are the 4 kinds of sentences with examples?

0 Answers  


What is the command to check wwn # in linux?

4 Answers   Siemens,


What is the difference between cd and cd in linux?

0 Answers  






what is the command to view gateway?

5 Answers  


What is top command in linux?

0 Answers  


what is QUOTA-V

4 Answers   Google,


What is tail command in linux?

0 Answers  


What does mkdir mean?

0 Answers  


You want to know how many lines in the kickoff file contains ‘prize’. Which of the following commands will produce the desired results?

0 Answers  


What is the difference between kill and kill -9 commands?

4 Answers   Sabre,


Categories