Shell functions and built-in variables

Source: Internet
Author: User

1, Special shell variable

$# the number of arguments passed to the script
$* displays all parameters passed to the script in a single string
$$ the current process ID number for the script to run
$! the ID number of the last process running in the background
[Email protected] is the same as $*, but is used with quotation marks and returns each parameter in quotation marks.
$-shows the current options that the shell uses, as is the SET command function.
$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.

Command > file redirects the output to file.
Command < file redirects the input to file.
Command >> file redirects the output to file in an append manner.
n > file redirects the file descriptor n files to filename.
n >> files redirect files with file descriptor N as an append to file.
N >& m merges the output file m and N.
N <& m merges the input file m and N.
<< tag will start to tag the contents of tags between tag and end tag as input.

$ command >/dev/null
/dev/null is a special file, and the content written to it is discarded, and if you try to read from the file, nothing is read. However, the/dev/null file is very useful and redirects the output of the command to it, which results in a "no output" effect.
If you want to mask stdout and command >/dev/null 2>&1

$ A script name
Location parameter #1
$-$9 positional parameter #2-#9
${10} positional Parameters #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
Process ID (PID) of the $$ script
$-the flags passed to the script (using set)
$_ the last parameter of the previous command
$! the Process ID (PID) of the last job running in the background

2, two Yuan comparison

-eq equals
= equals
= = equals
-ne Not equal to
! = does not equal
-lt less than
\< less than (ASCII) *
-le less than or equal to
-GT Greater than
\> greater than (ASCII) *
-ge greater than or equal to
-Z string is empty
-N string is not empty
> Greater than
>= greater than or equal to
< less than
<= less than or equal to

3, File test type

-e file is present
-S file size is not 0
-F is a standard file
-D is a directory
-r file with Read permission
-W file has write permissions
-X file has execute permissions
-h file is a symbolic link
-l file is a symbolic link
-B file is a block device
-c file is a character device
-G Sets the Sgid tag
-P file is a pipe
-U set the suid tag
-S file is a socket
-K Sets the "Paste bit"
-T file associated with a terminal
-N has been modified since the last time this file was read
-O The host of this file is your
-G file has the same group ID as the group you belong to
F1-nt F2 file F1 F2 new than file *
F1-ot F2 file F1 F2 older than file *
F1-ef F2 file F1 and file F2 are hard links to the same file *

!" Non "(reverses the test result above)

4, parameter substitution and extension

Meaning of an expression
${var} variable var value, same as $var

${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, 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+other} If Var is declared, then its value is $other, otherwise it will be a null string
${var:+other} If Var is set, then its value is $other, otherwise it will be a null string

${var? Err_msg} If Var is not declared, then print $err_msg *
${var:? Err_msg} If Var is not set, then print $err_msg *

${!varprefix*} matches all variables declared at the beginning of Varprefix
${[email protected]} matches all previous variables declared at the beginning of the Varprefix



${#string} Length of $string

${string:position} in $string, extract the substring from position $position
${string:position:length} in $string, start extracting substrings of length $length 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 $substring, then $replacement is used instead of the matching $substring
${string/%substring/replacement} If the $string suffix matches $substring, then $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 where the first character of the $substring that matches in $string appears
Expr substr $string $position $length to extract a substring of length $length from position $position in $string
Expr match "$string" \ ($substring \) ' Extracts $substring* from the beginning of $string
Expr "$string": ' \ ($substring \) ' Extract $substring* from the beginning of $string
Expr match "$string" '. *\ ($substring \) ' Extract $substring* from the end of $string
Expr "$string": '. *\ ($substring \) ' Extract $substring* from the end of $string

, 5, Brackets
If [CONDITION] test structure
if [[CONDITION]] Extended test structure
Array[1]=element1 Array Initialization
[A-z] the character range of a regular expression

, 6, curly braces
${variable} parameter substitution
${!variable} indirect variable reference
{Command1; command2;.. commandn;} code block
{String1,string2,string3,...} Curly brace Extension


, 7, round brackets
(Command1; command2) a command group executed in a child shell
array= (element1 element2 element3) array initialization
result=$ executes the command in the child shell and assigns the result to the variable
> (COMMAND) process substitution
< (COMMAND) process substitution

, 8, double parenthesis
Integer operation ((var = 78))
var=$ ((20 + 5)) integer operation with the result assigned to the variable

, 9, quotation marks
"$variable" "weak" reference
' String ' "strong" reference

10, post reference
result= ' command ' runs the command in the child shell and assigns the result to the variable



Shell functions and built-in 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.