Shell Special variables

Source: Internet
Author: User

    1. Positional variables

$ $ Gets the file name of the currently executing shell script, including the script path if the script is executed with a path

[[Email protected] shell]# sh n.sh n.sh[[email protected] ~]# sh/shell/n.sh/shell/n.sh[[email protected] shell]# sh ' pwd '/n.sh/shell/n.sh

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/99/2D/wKioL1lE6VOQrQ3wAABbP7bwXP0998.png-wh_500x0-wm_ 3-wmp_4-s_1472339245.png "title=" $0.png "alt=" Wkiol1le6voqrq3waabbp7bwxp0998.png-wh_50 "/>

$n   Gets the nth parameter value of the currently executing shell script, n=1..9, when n is 0 indicates the file name of the script, if n is greater than 0, Braces enclosed ${10}

 script content: #!/bin/bashecho $1[[email protected] shell]# sh  p.sh xcn      #1个参数xcn                                                                                                                          [[email  protected] shell]# sh p.sh xcn cainiao     #2个参数输出的是第一个参数xcn 

$* gets all parameters of the current shell, treats all command-line arguments as a single string, equivalent to "$1$2$3" ... Note with the $ #的区域


$# gets the total number of parameters that are followed by the currently executing shell script

[email protected] shell]# cat p.sh echo $1echo $#[[email protected] shell]# sh p.sh {a.. Z}A26 Enterprise Application: (Control the number of user parameter input) when the input parameter is not equal to 2 set up the output "muse", the side will not output "Buchengli" [[email protected] shell]# cat 1p.sh #!/bin/bash[$# -ne 2] && {echo "Muse" Exit 1 #值1是退出后 $? value}echo "Buchengli" [[email protected] shell]# sh 1p.sh baimu Se two[[email protected] shell]# sh 1p.sh bai shubuchengli[[email protected] shell]#

[email protected] All parameters of this program "$" "$" "$" "...", this is the best way to pass parameters to other programs because he retains any whitespace embedded in each parameter.


Examples of differences between $* and [email protected]

    • $* treats all command-line arguments as a single string, equivalent to "$1$2$3", "$*"

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

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

Shift Move Position variable


2. Process state variables

$$ Gets the process number of the current shell

$! PID that executes the previous instruction

$? Gets the return value that executes the last instruction (0 is successful, nonzero is failed) #这个变量很常用

$_ the last parameter of a command or script executed before this


This article is from the "Little Rookie" blog, please be sure to keep this source http://baishuchao.blog.51cto.com/12918589/1939495

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.