Saturday, 5 November 2016

What is difference between $*, $@ , $_and $0?

Q.46 . What is difference between $*, $@ , $_and $0?

$#
The number of arguments supplied to a script.
$*
All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2. 
The "$*" special parameter takes the entire list as one argument with spaces between
$@
All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
The "$@" special parameter takes the entire list and separates it into separate arguments.
$?
The exit status of the last command executed.
$0
Gives File Name


test.sh
#!/bin/sh
 
echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
Here is a sample run for the above script −

$./test.sh Foo Bar
File Name : ./test.sh
First Parameter : Foo
Second Parameter : Bar
Quoted Values: Foo Bar
Quoted Values: Foo Bar
Total Number of Parameters : 2


$echo $?
0
 

No comments:

Post a Comment

Tricks and Tips