Shell Special Variable character interception

Source: Internet
Author: User


Shell Special variables



Variable meaning

$ $ Gets the script name of the currently executing shell script, including the path and script name if the script has a path

$n Gets the nth parameter in the currently executing shell script. But when n=1..9, but n is 0, indicates the file name of the script, and if n is greater than 9, it needs to be enclosed in curly braces, such as ${10}.

$# Get the total number of parameters in the current shell command line

All positional parameters of "$*" (as a single string) get all the parameters of the current shell script and treat all command-line arguments as a string.

"[Email protected]" all positional parameters (each as separate strings) get all the parameters of the current shell script, and each parameter of all command lines is treated as a separate string.

${#*} the number of command-line arguments passed to the script

${#@} the number of command-line arguments passed to the script

$? Gets the return value that executes the last instruction (0 is a success value, not 0 is a failure)

$$ Gets the process ID (PID) of the current execution script

$- The flags passed to the script (using set)

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

$! Process ID (PID) of the last job running in the background

* must be quoted, otherwise the default is "[email protected]".


DirName #获取当前脚本路径

BaseName #获取当前脚本名称


The difference between $* and [email protected]

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

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

#############################################################

Variable Quantum string

Meaning of an expression

${#string} length of $string

${string:position} $string, extract substrings starting from position $position

${string:position:length} $string, extract the substring of length $length starting from position $position

${string#substring} removes the substring of the shortest match $substring from the beginning of the variable $string

${string# #substring} Remove the substring of the longest matching $substring from the beginning of the variable $string

${string%substring} removes the substring of the shortest match $substring from the end of the variable $string

${string%%substring} Remove the substring of the longest matching $substring from the end of the variable $string

${string/substring/replacement} uses $replacement instead of the first matching $substring

${string//substring/replacement} uses $replacement instead of all matching $substring

${string/#substring/replacement}

If the $string prefix matches the $substring, then the $replacement is used instead of the matching $substring


${string/%substring/replacement}

If the $string suffix matches the $substring, then the $replacement is used instead of the matching $substring


This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1701156

Shell Special Variable character interception

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.