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

What u know abt tar Command?

5 Answers   Locuz,


How many interfaces(ethernet cards) are working using single command?

0 Answers  


How can we increase disk read performance in single command in Linux?

0 Answers  


Explain Backup Managment commands in Linux.

5 Answers  


How do I check disk space in linux?

0 Answers  






What is the copy command in linux?

0 Answers  


Hi everyone i want to learn Devops in Hyderabad can anyone please suggest me best institute.

0 Answers  


How do you insert comments in the command line prompt?

0 Answers  


How do you limit memory usage for commands?

0 Answers  


How can we increase disk read performance in single command?

0 Answers  


how is "to run even after user logs out"

2 Answers   Google,


In Linux how you set time a limit on quota?

1 Answers   Mind Tree,


Categories