Tuesday, September 11, 2012

While scripting we need to pass previous command line arguments to new script which we are calling inside the first one. In this case, we are using $* and $@ parameters to pass previous arguments to new script. Take one example, about these parameters.
$vi test.sh
echo $*;
echo "#################";
echo $@;
$sh test.sh "First Argument" "Second Argument"
Output is as follows:
First Argument Second Argument
#################
First Argument Second Argument
With this example you are not able to understand the difference between them.
The actual difference between them is the $* considers all arguments as a one string and $@ considers the each quoted string as single argument. You will understand this thing by one more example.
$vi test.sh
for i in "$*"
do
echo $i ;
done
echo "for loop completed of \$*";
for i in "$@"
do
echo $i;
done
echo "for loop completed of \$@";
if you run this script with as :
$sh test.sh "First Argument" "Second Argument"
Output of this as follows:
First Argument Second Argument
for loop completed of $*
First Argument
Second Argument
For loop completed of $@
Above output shows that $* is considering all arguments as one string. And whole string assigns to one variable only. But $@ is considering quoted string as a single argument and displaying there two arguments as it is.             
Read more about the bash shell ....
Posted by Machindra Dharmadhikari On 9/11/2012 04:10:00 PM 2 comments

2 comments:

  1. Hi There,

    Interesting piece! Great to see someone write difference-between-and-in-shell. who is not a fanatic or a complete skeptic.


    I am hearing a lot in conferences and webinars lot about Micro services, self contained services, so does the software will require an Unix Linux OS or it will use the self contained services?

    I look forward to see your next updates.

    Obrigado,
    Preethi

    ReplyDelete

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

    Chitika Ads 2

    Histat

    About

    Enter your email address:

    Delivered by FeedBurner