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

our wish to print the file vacations with 60 lines to a page. Which command will accomplish this?

2 Answers  


How do you create a blank file in linux?

0 Answers  


How lilo is useful in linux?

0 Answers  


What is an os command?

0 Answers  


You have a computer with 80 GB hard disk and Ubuntu 8.04 is installed on entire hard disk. Now you have to create a seprate partition for Windows OS and Install Win Xp as Dual boot. write down the steps involed along with the commands.

8 Answers   Bhel, Indian Navy, Tata Steel Limited,






You wish to print a file ‘draft’ with 60 lines on a page. What command would you use?

0 Answers  


How do you limit memory usage for commands?

0 Answers  


What is an inode?

3 Answers   ProdEx Technologies,


How do I find cpu in linux?

0 Answers  


How do you know which shell I am using in linux?

0 Answers  


What is df command in unix?

0 Answers  


How to write the output of a command to a file?

0 Answers  


Categories