What is the difference between $ @ and $ * in shell learning?

Source: Internet
Author: User

What is the difference between $ @ and $ * in shell learning?

Before talking about $ @ and $ *, you must start with the positional parameter of shell script...
We all know how variables are defined and replaced.
However, we also need to know that some variables are set in shell, and their names cannot be modified at will,
Positional parameter is included. In shell script, variables such as $0, $1, $2, $3... can be used to extract the parameters in the command line,

For example, if ls-l ls is $0 and-l is $1

First, it is $ #: it can capture the number of positional parameter.

The previous my. sh p1 "p2 p3" is used as an example:

Because the IFS between p2 and p3 is in soft quote, $ # can get the value of 2.

Next is $ @ and $ *:
Specifically, the two are only different in soft quotes. Otherwise, they all represent "All Parameters" (except $0 ).
For example, if you run my. sh p1 "p2 p3" p4 on command line,
For $ @ or $ *, you can get p1 p2 p3 p4.
However, if it is placed in soft quotes:
"$ @", You can get the three different word segments "p1" "p2 p3" "p4 );
"$ *", You can obtain the entire string of "p1 p2 p3 p4.

Modify my. sh to make the content as follows:
Code:
#! /Bin/bash
My_fun (){
Echo "$ #"
}
Echo 'the number of parameter in "$ @" is '$ (my_fun "$ @")
Echo 'the number of parameter in "$ *" is '$ (my_fun "$ *")
Then run./my. sh p1 "p2 p3" p4 to know where the difference between $ @ and $ * is... ^_^.

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.