Shell common list of special variables

Source: Internet
Author: User



Variable Meaning
$ Gets the file name of the current execution shell script
$n Arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is $ A. When n is greater than 9 o'clock it needs to be enclosed in curly braces, such as: ${10}
$# The number of arguments passed to the script or function.
$* Get all the parameters of the current shell, note the difference from [email protected]
[Email protected] All parameters of this program "$" "$" "$" "...", this is the best way to pass parameters to other programs, because he will keep all the blanks embedded in each parameter
$ The exit state of the last command, or the return value of the function.
$$ The current shell process ID. For Shell scripts, this is the process ID where the scripts are located.


Command-line arguments

The arguments passed to the script when the script is run are called command-line arguments. Command-line arguments are represented by a $n, for example, $ = for the first argument, and $ for the second argument, and so on.

Take a look at the following script:

#!/bin/bashecho "File Name: $" echo "first Parameter:" Echo "first Parameter: $" echo "Quoted Values: [Email protected ] "echo" Quoted Values: $* "echo" Total number of Parameters: $# "

Operation Result:

[Email protected] scripts]# sh test.sh linux thinkerfile Name:test.sh first Parameter:linux first Parameter:thinke R Quoted values:linux thinker Quoted values:linux thinker total number of parameters:2
The difference between $* and [email protected]

Both

$* and [email protected] represent all parameters passed to a function or script, and are not enclosed by double quotation marks (""), with "$" "$" ... All parameters are output in the form "$n".

But when they are enclosed in double quotation marks (""), "$*" takes all parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the parameters to "$" "$" ... All parameters are output in the form "$n".

#!/bin/bash echo "\$*=" $* # # # "\" for escaping echo "\" \$*\ "=" $* "echo" \[email protected]= "[email Protected]echo" \ "\[email Pro Tected]\ "=" [email protected] "echo" Print each param from \$* ' for Var in $*do Echo ' $var ' doneecho ' Print each param fr Om \[email protected] "for Var in [e-mail protected]do echo" $var "Doneecho" print each param from \ "\$*\" "for Var in" $* " Do echo ' $var ' doneecho ' Print each param from \ ' \[email protected]\ ' ' for var in ' [email protected] ' do echo $var ' Don E

Execute./test2.sh "1" "2" "3" "4" and see the following result:

[[Email protected] scripts]# sh test2.sh "1" "2" "3" "4" $*= 1 2 3 4 "$*" = 1 2 3 4[email protected]= 1 2 3 4 "[Email protecte D] "= 1 2 3 4print each param from $*1234print to param from [email protected]1234print to param from" $* "1 2 3 4print Each param from "[email protected]" 1234
Exit status

$? You can get the exit status of the previous command. The so-called exit status is the return result after the last command was executed.

Exit status is a number, in general, most of the command execution succeeds returns 0, and the failure returns 1.

However, there are some commands that return other values that represent different types of errors.

In the following example, the command executes successfully:

[[Email protected] scripts]# sh test.sh thinker linuxfile Name:test.shFirst Parameter:thinkerfirst parameter:linuxquo Ted Values:thinker linuxquoted values:thinker linuxtotal number of parameters:2[[email protected] scripts]# echo $?0

This article is from the "Farmer Uncle Learn Linux" blog, please be sure to keep this source http://peasantry.blog.51cto.com/12131934/1889333

Shell common list of 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.