Special variables for Shell programming learning notes ($, $, $, $, $#, $@, $*)

Source: Internet
Author: User

Special variables ($, $, $, $, $#, [email protected], $*)

There are some special variables that can be used in shell programming. These variables can be used as global variables in the script.

Name Description
$ Script Name
$1-9 Parameter 1 to parameter 9 when the script executes
$? The return value of the script
$# The number of arguments entered when the script executes
[Email protected] The specific contents of the input parameters (the input parameter as a multiple object, which is a list of all parameters)
$* The specific contents of the input parameters (the input parameters as a single word )

[Email protected] The difference with $*:

[email protected] and $* can use a variable to represent all of the parameter contents, but there are some differences between the two variables.

[Email protected]: Use input parameters as a list object

$*: Takes the input parameter as a word

Source code:
1 Echo "the name of the script is:"$02n=13 Echo "the list of parameters using \[email protected] is:"[email protected]4 forTemstrinch "[email protected]"5 Do6 Echo "The first $n parameters are:"$temstr7Let n+=18 Done9 Ten Onen=1 A Echo "the parameter list using \$* is:"$* - forTemstrinch "$*" - Do the Echo "The first $n parameters are:"$temstr -Let n+=1 - Done
Execution Result:
1$./test.SH 1 2 3 42The name of the script is:./test.SH3The list of parameters using [email protected] is:1 2 3 44The 1th parameter is:15The 2nd parameter is:26The 3rd parameter is:37The 4th parameter is:48The parameter list using $* is:1 2 3 49The 1th parameter is:1 2 3 4

In the example above, using [email protected] and $* are both quoted in double quotes , but when $* does not use double quotes, the result is the same as [email protected] .

Cause Analysis:

When using double quotation marks, we can see that the parameter list output is the same, it is true that two variables can store all the contents of the parameters, but also shows that the parameters of the two variables of the different processing, that is, one to treat as a list, one to speak all parameters as a word processing.

When $* is not in double quotes, when executed to the For statement, the value of $* (1 2 3 4) is first removed, and then the loop statement becomes the for tem in 1 2 3 4, and the final output becomes the loop output of the list.

1 Source: 2 #!/bin/bash  3 n=1  4 for in $* 5 do 6     Echo $n is $tem 7     Let n+=1  8 done 9
Execution Result:
#./test. SH 1 2 3 4 1 1 2 2 3 3 4 4

Here 's the difference between single quotes, double quotes, and no quotes :

Single quotes:

What you can say is what you see is what you get: the contents of a single quotation mark are output as is, or what you see in single quotes is what you output.

Double quotes:

Output the contents of double quotation marks, if there are commands in the content, variables, etc., will first parse the variables, commands, and then output the final content.

The command or variable is written as ' command or variable ' or $ (command or variable) in double quotation marks.

No quotation marks:

The output of the content may not be considered as a whole output of a string containing a space;

If there are commands, variables, etc. in the content, the variables and commands are parsed and then the final content is output.

If the string with special characters such as spaces, you can not complete the output, you need to add double quotation marks, general continuous string, number, path, etc. can be used, but it is best to use double quotation marks instead of

Special variables for Shell programming learning notes ($, $, $, $, $#,[email protected], $*)

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.