Note-shell Special variables

Source: Internet
Author: User


Positional variables:

$: Gets the file name of the current execution shell, including the script path if the script is executed with a path

$n: Gets the n=1..9 parameter value of the currently executing shell script, which represents the file name of the script when n is 0, and if n is greater than 9, the curly braces are enclosed in ${10}.

[email protected] ~]# vim q.sh
echo $ $4 $ $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}


Results of the output

[email protected] ~]# sh q.sh {a.. Z
A b c d e F g h i j k l m n o


$*: Treats all command-line parameters as a single string, equivalent to "$1$2$3", "$*"

[email protected]: Treat each parameter of the command line as a separate string, equivalent to "$!" $ "$" This is the best way to pass parameters to other programs because he retains any whitespace embedded in each parameter

Note : The above differences are limited to double quotes, i.e. "$*" and "[email protected]"

Example:[[email protected] ~]# Set--"I am" from Hunan

[email protected] ~]# echo $#

3


Example :

[[email protected]~]# for I in "$*";d o echo $i;d one with double quotes, as a parameter output
I am from Hunan
[[email protected]~]# for i in "[email protected]";d o echo $i;d one with double quotes, independent output
I am
From
Hunan

Example:

[[email protected] ~]# for I;do Echo $i;d One removes the in variable list, equivalent in "[email protected]"

I am


From


Hunan

Example:

[[email protected] ~]# for i in $*;d o echo $i;d one without double quotes, all parameters are output, and the first parameter "I Am" is also opened

I
Am
From
Hunan



$?: Gets the return value that executes the last instruction (0 is successful, nonzero is failed) This variable is very common.

Cases:

echo $?

$#: Gets the total number of parameters after the currently executing shell script.


Enterprise Case Control the number of user parameters :

                   [ [email protected]  ~]# vim q1.sh

[$#-ne 2] && {
echo "Pls input number"
Exit 1----> assigns a value to the current shell's $? variable.

}
echo "Thanks!"

Note-shell Special variables

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.