"Shell script Learning" $* and $@

Source: Internet
Author: User

Both $* and $@ variables provide fast access to all parameters, both of which can store all command-line arguments in a single variable.

The $* variable saves all parameters provided on the command line as a single word and is treated as a single argument instead of multiple objects.

The $@ variable treats all the arguments provided on the command line as multiple separate words in the agreed string. He allows you to iterate through all the values and separate each parameter provided. This is usually done through the for command.

Here is an example to see the ~

root@wl-ms-7673:/home/wl/Desktop/shell# cat-n test.sh 
     1	    #!/bin/bash  
     2	    echo "\$* and \$@ Test"  
     3	    Echo ' \$* is: ' $* #这里两个输出结果是一样的  
     4	    Echo ' \$@ is: ' $@ #  
     5	    count=0  
     6 for	    var in ' $* '  
     7	    do  
     8	        count=$[$count +1]  
     9	        echo "$count:" $var	    A echo "\$* done."	      
    count=0 for	    var  
    in	    "$@"	    todo	        count=$[$count +1]	        echo "$count:" $var  
    "	    echo" \$@ done. "  
root@wl-ms-7673:/home/wl/Desktop/shell# 
The results of running the output are as follows:

root@wl-ms-7673:/home/wl/Desktop/shell#./test.sh a b c d E F
$* and $@ test
$* is:a b c d E F
$@ is:a b c D e f
  1:a b c d E F
$* done.
1:a
2:b
3:c 4:d 5:e 6:f done
.
root@wl-ms-7673:/home/wl/Desktop/shell# 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.