Linux shell Bash built-in variable reference

Source: Internet
Author: User
Tags arithmetic

      • Special Shell variables
      • Test operation: Two Yuan comparison
        • Arithmetic comparison
        • string comparison
      • File type operations
      • Parameter substitution and extension
      • String manipulation
      • Summary of some structures

Special Shell variables
variables meaning
$ Foot Name
$-$9 Position parameter 1-9
${10} Position parameter 10
$# Number of positional parameters
"$*" All positional parameters (as a single string)
"[Email protected]" All positional parameters (each as a separate string)
${#*} The number of command-line arguments passed to the script
${#*} The number of command-line arguments passed to the script
$? return value
$$ PID of the script process
$- The identity passed to the script
$_ Last parameter of the previous command
$! Process ID (PID) of the last job running in the background
Test operation: Two USD compare arithmetic comparison
operator meaning
-eq Equals
-ne Not equal to
-lt or ((<)) Less than
-le or ((<=)) Less than or equal
-gt or ((>)) Greater than
-ge Greater than or equal
string comparison
operator meaning
=
==
!=
< Less than (ASCII)
> Greater than (ASCII)
-Z String is empty
-N string is not empty

If you use it in a double-bracket [[...]] test structure, you do not need to use the escape character \.

File type operations
operator meaning
-E File is
-F Standard file
-D Directory
-H Hard Link Link
-L Link
-B Block devices
-C Character device
-P Pipeline
-S Socket sockets
-T File is associated with a terminal
-N The file has been modified since the last time it was read
-O The document belongs to the Lord himself.
-G Group ID is consistent with performer
-S The file size is not 0
-R File has Read permission
-W File has Write permissions
-X File has Execute permissions
-G Set the Sgid tag
-U Set the suid tag
-K Set the paste position
File1-nt File2 File1 than file File2 new
File1-ot File2 File1 than file2 old
File1-ef File2 Fiel1 and Fiel2 are hard links to the same file
Parameter substitution and extension /tr>
expression meaning
${va R_default} if Var is not declared, then use $default as its value *
${var=default} if Var is not declared, Then use $default as its value *
${var:-default} if Var is not declared, or its value is empty, then use $default as its value *
${var:=default} if Var is not declared, or its value is empty, use $default as its value *
${var+ot Her} if Var is declared, then its value is $other, otherwise it will be a null string
${var:+other} if Var is set, then its The value is $other, otherwise the null string
${var? ERR_MSG} if Var is not declared, print $err_msg*
${var:? ERR_MSG} if Var is not set, print $err_msg*
${!varprefix*} match before all start with Varprefix Declared variable
${[email protected]} matches all variables declared at the beginning of Varprefix
String manipulation
An expression meaning
${#string} Length of $string
${string:position} In $string, start extracting substrings from position $position
${string:position:length} In $string, a substring of length $length is extracted starting from position $position
${string#substring} Delete the substring of the shortest match $substring from the beginning of the variable $string
${string##substring} Delete the substring of the longest matching $substring from the beginning of the variable $string
${string%substring} Delete the substring of the shortest match $substring from the end of the variable $string
${string%%substring} Delete the substring of the longest matching $substring from the end of the variable $string
${string/substring/replacement} Use $replacement to replace the first matching $substring
${string//substring/replacement} Use $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
expr match "$string" ‘$substring‘ Matches the length of the $substring* at the beginning of the $string
expr "$string" : ‘$substring‘ Matches the length of the $substring* at the beginning of the $string
expr index "$string" $substring The position in $string where the first character of the $substring is matched to
expr substr $string $position $length Extracting substrings of length $length from position $position in $string
expr match "$string" ‘\($substring\)‘ Extract the $substring* from the $string's opening position
expr "$string" : ‘\($substring\)‘ Extract the $substring* from the $string's opening position
expr match "$string" ‘.*\($substring\)‘ Extract $substring* from the end of $string
expr "$string" : ‘.*\($substring\)‘ Extract $substring* from the end of $string

$substring is a regular expression.

Summary of some structures
An expression meaning
if [CONDITION] Test structure
if [[CONDITION]] Extended Test structure
Array[1]=element1 Array initialization
[A-z] The character range of a regular expression
${!variable} Indirect variable Reference
{Command1; command2;.. commandn;} code block
{String1,string2,string3,...} Curly brace Extension
(Command1; command2) Command groups executed in a child shell
array= (element1 element2 element3) Array initialization
result=$ (COMMAND) Executes the command in the child shell and assigns the result to the variable
> (COMMAND) Process substitution
< (COMMAND) Process substitution
(var = 78) Integral type operation
var=$ ((20 + 5)) Integer operation and assigns the result to the variable
"$variable"
result=COMMAND Run a command in a child shell and assign the result to a variable

Reference http://blog.csdn.net/u012749933/article/details/48030503

Linux shell Bash built-in variable reference

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.