Basic analysis of Linux operating system (vii)--bash (Shell) Basics (4)

Source: Internet
Author: User
Tags arithmetic locale uppercase letter


Ix. unfolding

The expansion of the command line is done after splitting the word into words. There are seven types of expansion: curly brace expansion, wavy line expansion, parametric and variable expansion, command substitution, arithmetic expansion, branch word, and path expansion.

The order of expansion is: curly brace expansion, wavy line expansion, parameter and variable expansion, command substitution, arithmetic expansion, branch word, path expansion, in left-to-right order.

There is also an additional expansion available on the system: process substitution.

Only curly braces unfold, and the number of words and paths unfolding in the branch is changed before and after the expansion; Other expansion always expands a word into one word. The only exceptions are the "[email protected]" and "${name[@]}" mentioned above.

Curly braces unfold

Curly brace expansion is a mechanism that can produce arbitrary strings.  This mechanism is similar to the path expansion, but there is no need for a corresponding file. The pattern expanded by curly braces is an optional leading character followed by a series of comma-delimited strings or a set of expressions in a pair of curly braces, followed by an optional PostScript. The preamble is added in front of each string in curly braces, and the PostScript is appended to each result string and is expanded from left to right.

Curly brace expansion can be nested. The result of the expanded string is not sorted, but left-to-right order is preserved. For example: A{d,c,b}e expands to "Ade Ace Abe".

You can use {x.. y[. INCR]} to represent an ordered sequence of integers. This means that the arithmetic progression from X to Y ends with "incr" as the incremental amount. X can be 0, "incr" is optional, if not specified, the default is "1", which increments by 1 increments. If x and Y are the same, only one number is represented, and if "incr" is 0, the value is not valid and is still numerically incremented with the default value of "1". For example, {1..10} represents 10 integers from 1 to 10, {1..10..2} represents all odd numbers from 1 to 10, {2..10..2} represents all an even number from 2 to 10. Of course, you can also make x larger than Y, so that you can get a set of numbers from large to small in reverse order, such as: {100..1} is all integers that count from 100 to 1. Whether it is x or Y, as long as it represents a decimal, it can be "0". The default "INCR" is "1" or "1".

The curly brace expansion is done before any other expansion, and any characters that have special meaning to the other expansion remain in the result. It is carried out strictly literally. Bash does not make any semantic interpretation of the expanded context or the text in curly braces.

The correct curly brace expansion must contain no referenced left curly brackets and closing curly braces, and at least one comma that is not referenced. Any incorrect expressions are not expanded and remain intact. You can use a backslash to refer to "{" or ",". To prevent them from being recognized as part of a curly brace expression. To avoid conflicts with argument expansion, the string "${" is not considered a valid combination for curly braces.

This structure is usually abbreviated when the normal prefix generated by a string is much longer than the previous example. For example:

Mkdir/usr/local/src/bash/{old,new,dist,bugs}

Or:

Chown Root/usr/{ucb/{ex,edit},lib/{ex?.? *,HOW_EX}}

Curly brace expansion resulted in a little incompatibility with the historical version of Sh. When an opening or closing parenthesis appears as part of a word, SH does not treat them specially and retains them in the output. Bash removes the parentheses from the curly braces to expand the result of the word. For example, entering file{1,2} into SH will cause the output to be unchanged. The same input will output file1 and file2 after bash is expanded. If you need to strictly maintain compatibility with SH, you need to use the +B option when you start bash, or use the SET command plus the +B option to disable curly brace expansion.


Wavy line Expansion

If a word starts with a wavy line character (~) that does not have a reference, all characters before the first non-referenced slash (/) (or all characters of the word, if there are no unreferenced slashes) are considered to be wavy line prefixes. If there are no quoted characters in the tilde prefix, then the string after the wavy line is considered to be the login name. If the login name is an empty string, the wavy line will be replaced with the value of the shell parameter home. If home is not defined, the home directory of the user who is executing the shell is replaced. Otherwise, the tilde prefix is replaced with the home directory associated with the specified login name.

If the tilde prefix is "~+", it will be replaced with the value of the shell variable pwd. If the tilde prefix is "~-" and the shell variable oldpwd is set, the variable value will be used to replace it. If in the wavy line prefix, the string after the wavy line consists of a number n, the prefix optional "+" or "-", then the wavy line prefix will be replaced with the corresponding element in the directory stack, that is, the wavy line prefix as a parameter to perform the built-in command dirs display results. If the character after the wavy line in the tilde prefix is a number without a prefix, then it is assumed that there is a "+".

If the login name is illegal, or the wavy line fails, the word will not change.

In a variable assignment, the string after ":" or "=" is immediately checked with an unreferenced wavy line prefix. In this case, wavy lines are still expanded. Therefore, you can assign values to path, Mailpath, and Cdpath by using a file name with wavy lines, and the shell will give the expanded value.


Parameter expansion

The character "$" introduces parameter expansion, command substitution, and arithmetic expansion. The parameter name or symbol to be expanded is enclosed in curly braces, which is optional, but allows the variable to be expanded to be part of the variable name that does not conform to the word immediately followed.

When using curly braces, the closing parenthesis of the match must be the first one and is not referenced by a backslash or quotation mark '} ', nor is it contained in an embedded arithmetic expansion, command substitution, or parameter expansion.

${parameter}

The value to be replaced with parameter. If parameter is a positional parameter, and the number is more than one, the curly braces must be added when there are characters that are not part of the name immediately following parameter.

If the first character of parameter is an exclamation point, an indirect variable is introduced. Bash uses the value of the variable whose name is the remainder of parameter as the name of the variable, and then the new variable is expanded, and its value is used in subsequent substitutions instead of using the value of parameter itself. This is also known as indirect expansion. ${!prefix*} and ${!name[@]} is an exceptional case.

In each of the following cases, word is expanded with wavy lines, parametric expansion, command substitution, and arithmetic expansion. If the substring is not expanded, bash tests a parameter that has no definition or null, and the result of ignoring the colon is to test only undefined parameters.

${parameter:-word}

Use the default value. If parameter is undefined or the value is empty, the expansion of word is replaced. Otherwise, the value will be replaced with parameter.

${parameter:=word}

Assigns a default value. If parameter is undefined or the value is empty, the expansion of Word will be given to parameter. The value of the parameter will be replaced. Positional parameters and special parameters cannot be assigned this way.

${parameter:?word}

Displays an error, whether undefined or null. If parameter is undefined or the value is empty, the expansion of Word is written to a standard error, or exits if the shell is not interactive. Otherwise, the value of parameter will be replaced.

${parameter:+word}

Use an optional value. If parameter is undefined or non-empty, no substitution is made, otherwise it is replaced with the value after word expands.

${parameter:offset}

${parameter:offset:length}

Sub-string expansion. Expands the maximum length of characters to parameter, starting with the character specified by offset. If length is omitted, the substring expanded to parameter begins with the string specified by offset. Length and offset are arithmetic expressions. Length must be a value greater than or equal to 0. If the offset evaluation result is less than 0, the value is treated as an offset from the end of the value of parameter. If parameter is @, the result is a length positional parameter, starting with offset. If parameter is an array name with an "@" or "*" index, the result is a length member of the array, starting with the ${parameter[offset]}. The subscript for the substring starts at 0, and the subscript starts at 1, unless the positional parameter is used.

${!prefix*}

${[email protected]}

Name match prefix. Expands the variable name to start with the name prefix, separated by the first character of the special variable IFS. When "@" is used and is expanded in double quotes, each variable name is expanded into a single word.

${!name[@]}

${!name[*]}

The list displays the subscript for the array. If name is an array variable, expand to the lower label of the element that is assigned in name. If name is not an array, expand to 0. If "@" is used and is expanded in double quotes, each variable name is expanded into a single word.

${#parameter}

Parameter length. The length (number of characters) of the value to replace with parameter. If parameter is * or @, the replacement value is the number of positional arguments. If parameter is an array name, the subscript is * or @, and the replacement value is the number of elements in the array.

${parameter#word}

${parameter# #word}

Deletes the matching prefix pattern. Word is expanded into a pattern, just like in the path expansion. If this pattern matches the start of the value of the parameter, the result of the expansion is the shortest match (#的情况) of the parameter expanded value. or the longest match (' # # ' case) deletes the result. If parameter is @ or *, the mode delete operation is applied to each positional parameter in turn, and finally expands to the list of results. If parameter is an array variable, the subscript is @ or *, the pattern deletion is applied sequentially to each member in the array, and finally expands to the list of results.

${parameter%word}

${parameter%%word}

Removes the matching suffix pattern. Word is expanded into a pattern, just like in the path expansion. If this pattern matches the end of the value after the parameter expansion, the result of the expansion is the result of the deletion of the parameter expanded value, the shortest match (% of the case), or the longest match (percent of the case). If parameter is @ or *, the mode delete operation is applied to each positional parameter in turn, and finally expands to the list of results. If parameter is an array variable, the subscript is @ or *, the pattern deletion is applied sequentially to each member in the array, and finally expands to the list of results.

${parameter/pattern/string}

${/parameter/pattern/string}

Mode substitution. Pattern is unfolded as a pattern, just as the path unfolds. parameter is expanded, and the longest matching pattern in its value is replaced with a string. If the pattern starts with a "/", all matches are replaced by a string. Under normal circumstances, the value has the first match to be replaced by a string. If pattern starts with #, it must match the header of the parameter expanded value. If pattern starts with%, it must match the end of the value after the parameter expansion. If the string is null, the pattern match will be deleted, and the/after pattern will be ignored. If parameter is @ or *, then the Replace operation is applied to each positional parameter in turn, and finally expands to the list of results. If parameter is an array variable, the subscript is @ or *, the pattern deletion is applied sequentially to each member in the array, and finally expands to the list of results.

${parameter^pattern}

${parameter^^pattern}

${parameter,pattern}

${parameter,,pattern}

Case modification. This expansion modifies the case of alphabetic characters in the Patameter. Pattern is unfolded as a pattern, just as the path unfolds. The "^" operator matches the lowercase letter into uppercase letters; "," The operator matches the uppercase letter to lowercase letters; "^^" and "," expand the conversion of each character that matches in the expanded value. "^" and "," convert only the first character in the unwind value. If the pattern is skipped, it is treated like this, which will match every character. If parameter is "@" or "*", the case overwrite operation is executed sequentially on each positional parameter, and the resulting list is expanded. If parameter is an array variable subscript is "@" or "*", the case modification is performed sequentially on each array member, and the final result list is expanded.


Command substitution

Command substitution allows the command name to be replaced with the output of the command. There are two kinds of forms:

$ (command)

' Command '

Bash implements the deployment by executing a command and replacing the command itself with its standard output, and removes all subsequent newline characters. Inline newline characters are not deleted, but they may be deleted in the split of the word. The command replacement $ (cat file) can be replaced with an equivalent but faster method of $ (< file).

When replacing a form with an old-fashioned anti-quote "'", the backslash has only its literal meaning, unless it is followed by $, ', or \. The first backslash without a backslash will end the command substitution. When you use the $ (command) Form, all the characters in the parentheses make up the entire command, and no characters are handled specially.

Command substitution can be nested. To nest in the form of an inverted quotation mark, you can use a backslash to escape the inner layer of the back quotation marks.

If the substitution occurs in double quotation marks, the result is that the split and path of the word will not be expanded.


Arithmetic expansion

Arithmetic expansion allows the evaluation of arithmetic expressions and substitution of results. The format of the arithmetic expansion is:

$ ((expression))

Expression expressions are treated as if they were in double quotes, but the double quotation marks in parentheses are not handled in a special way. All the words in the expression have been expanded by parameters, string expansion, command substitution, and reference deletion. Arithmetic substitutions can be nested.

Bash is evaluated against the rules, and if the expression is not valid, a message is displayed to indicate that the failure does not replace the operation


Process substitution

Process substitution is only available in the system when you support Named pipes (FIFOs) or when you open a file by using the named Method/dev/fd. It is in the form of < (list) or > (list). The input or output of the process list runtime is connected to a file in a FIFO or/DEV/FD. The file name is passed to the current command as a parameter to expand the result's identity. If you use the > (list) Form, writing to a file is the input to the list. If you use the < (list) Form, the file is passed as a parameter that should be read to get the output of the list.
If possible, process substitution is performed at the same time as parameter and variable expansion, command substitution, and arithmetic expansion.


Word splitting

The shell detects the results of the parameter expansion, command substitution, and arithmetic expansion that do not occur in double quotation marks in order to divide the word.

The shell nonalphanumeric each word of IFS as a delimiter, dividing the other unfolded results into words based on these characters. If IFS is not defined, or if its value is "<space>" <tab> "<newline>", then by default a series of "<space>" occurrences at the beginning or end of the previously expanded results "<tab>" "<newline>" will be ignored, and any sequence of IFS characters that are not at the beginning and end will be used for word delimitation. If the value of the IFS is a value other than the default, then the whitespace characters "<space>" and "<tab>" at the beginning and end of the word are ignored, as long as the whitespace character is within the value of the IFS (that is, IFS contains white space characters). Any character that is within the IFS but not the IFS whitespace character, and any adjacent ifs whitespace characters, separates the fields. A series of IFS whitespace characters are also used as delimiters. If the value of IFS is empty, word splitting is not processed.

An explicitly given null parameter ("" or ") will be retained. An implicitly null parameter that is not referenced from a parameter that is not valued is deleted. If an argument with no value expands in a double quote reference, the null parameter is preserved as the result.

NoteIf you do not perform an expansion, the word split is not performed.


Path name expansion

After the word is split, bash searches for each word, looking for the character "*", "?", unless the-f option is set. and "[". If any of these appear, then the word is treated as a pattern, and the alphabetical order replaces the list of filenames of the matching pattern. If you do not find a matching file name, and the shell disables the Nullglob option, the word will not change. If the NULLGLOB option is set but no match is found, the word will be deleted. If the FAILGLOB option is set but no match is found, an error message is displayed and the command is not executed. If the shell has the NOCASEGLOB option enabled, the case of the letter will not be considered when matching. When the pattern is used for pathname expansion, the character "." At the beginning of a name or immediately following a slash, it must be explicitly matched unless the Shell's Dotglob option is set. When a path name is matched, the slash character must always be explicitly matched. In other cases, the character "." No special treatment is done.

The shell's environment variable globignore can be used to strictly restrict a series of file names that are matched by a pattern. If Globignore is set, each matching file name matches any of the patterns in Globignore and is removed from the matching list. When Globignore is defined and not empty, the filename "." and ".." is always ignored. However, setting the environment variable Globignore to a non-null value has the effect of opening Dotglob this shell option, so all other "." The starting file name will be matched. In order to get ignored with "." Start with the behavior of the file name, you can add ". *" as one of the globignore modes. The DOTGLOB option is disabled when Globignore is not defined.


Pattern matching

Any character appearing in the pattern, except for the special pattern character described below, matches itself only. The "NUL" character cannot appear in the mode. Backslashes are used to escape the following characters; the backslash used to escape is discarded when matched. If you want to match a literal special pattern character, it must be quoted in quotation marks.

Special mode characters have the following meanings:

*

Matches any string containing empty strings. When the shell's Globstar option is opened, "*" is used for the pathname expansion context, two adjacent "*s" as a single pattern that matches all files, 0 or more directories, and subdirectories. If followed by a "/", two adjacent "*s" can only match directories and subdirectories.

? matches any single character.

[...]

Matches any single character that is enclosed by a pair of "[]". A pair of characters separated by hyphens (-) represents a range expression, and any characters that are arranged between them, including the sort order and character set characters that use the current locale, are matched. If the first character after "[" is a "!" or a "^", then any characters that are not included will be matched. The order of the characters in the range expression is determined by the value of the current locale and environment variable lc_collate (if set). "-" can only be matched as the first or last character in the collection. A "]" can only be matched if it is the first character in a collection.

In "[" and "]", the character class can be specified with [: Class:] syntax, where class is one of the following class names defined in the POSIX.2 standard:

Alnum Alpha ASCII blank cntrl digit graph lower print punct space upper word xdigit

A character class matches any character that belongs to this class. The keyword character class matches letters, numbers, and "_" characters.

In "[" and "]", you can use syntax such as [=c=] to specify equivalence classes. It matches characters that have the same merge weights as the character "C" (defined by the current locale).

In "[" and "]", syntax [. symbol.] Match merge symbols symbol.

Several extended pattern-matching operators need to be recognized. In the following description, "Pattern-list" is one or more patterns with the "|" A delimited list. The composite mode can be constructed using one or more of the following sub-modes:

? (pattern-list)

Match the given pattern 0 or one time

* (Pattern-list)

Matches the given pattern 0 or more times

+ (Pattern-list)

Match the given mode one or more occurrences

@ (pattern-list)

Match exactly one of the given patterns

The following pattern matching operators are also recognized if the EXTGLOB option for the built-in command shopt is turned on:

! (pattern-list)

Anything except a string that matches one of the given patterns


Reference removal

After the preceding expansion, all unreferenced characters \, ', and not all characters in the expanded result above are deleted.


Ten, redirect

Before the command executes, its inputs and outputs may be redirected by the shell using a special notation to explain it. Redirection can also be used to open and close files for the current shell execution environment. The following redirection operators can be either pre-positioned or placed in a simple command, or placed after the command. Redirects are processed in the order in which they appear, from left to right.

Each redirect that may precede the file descriptor number replaces the word leading in the form {VARNAEM}. In this case, the shell assigns a file descriptor greater than or equal to 10 for each redirect operator except for ">&-" and "<&-," and assigns a value to varname. If ">&-" and "<&-" precede {varname}, the value of the file descriptor defined by VarName ends here.

In the following description, if the file descriptor is skipped and the first redirect operator is "<", then redirection refers to the standard input (the file descriptor is 0). If the first character of the redirect operator is a, then redirection refers to the standard output (the file descriptor is 1).

In the following description, the words after the redirect operator are expanded with curly braces, wavy lines, parametric expansion, command substitution, arithmetic expansion, removal of references, path expansion, and Word division without special instructions. If you expand to more than one word, bash will give an error.

It is important to note the order of redirects. For example, the command

ls > Dirlist 2>&1

Directs standard output and standard errors to file dirlist, while the command

LS 2>&1 > Dirlist

Standard output is only directed to file Dirlist because standard errors are copied to standard output until standard output is redirected to file dirlist.

Some filenames are specially handled by bash in redirection, as shown in the following table:

/dev/fd/fd

If the FD is a valid integer, the file descriptor fd is copied.

/dev/stdin

File descriptor 0 is copied.

/dev/stdout

File descriptor 1 is copied.

/dev/stderr

File descriptor 2 is copied

/dev/tcp/host/port

If host is a legitimate host name or IP address, and port is an integer port number or service name, Bash attempts to establish a TCP connection to the corresponding socket.

/dev/udp/host/port

If host is a legitimate host name or IP address, and port is an integer port number or service name, Bash attempts to establish a UDP connection to the appropriate socket.

Failure to open or create a file will cause redirection to fail.

Redirection with file descriptors greater than 9 should be used with caution, as they may conflict with file descriptors used inside the shell.

REDIRECT Input

Redirection input causes a file with the name expanded as "word" to be opened and read by the file descriptor N, and if n is not specified, it is read as a standard input (file descriptor 0).

The general form of redirection input is:

[N]<word


REDIRECT Output

Redirected output causes a file with the name "word" to expand to be opened and written by the file descriptor N, if n is not specified, then

Written as standard output (file description identifier). Created if the file does not exist, or truncated to 0 bytes if the file does exist.

The general form of redirected output is:

[N]>word

If the redirect operator is ">" and the Noclobber option for the built-in command set is enabled, redirection will fail if the resulting file name exists and is a normal file when Word expands. If the redirection operator is ">|", or if the redirection operator is ">" and there is no noclobber option to enable the built-in command set, the redirect will be attempted even if Word has the file name.


Append REDIRECT Output

The output redirection in this way makes the file with the word expand result name open and is added from the tail by the file descriptor N. If n is not specified, the standard output (file descriptor 1) is used. If the file does not exist, it is created.

The general form of redirection is:

[N]>>word

REDIRECT standard output and standard error

Bash allows the use of this structure to redirect standard output and standard errors (file descriptors 1 and 2) to files that have the word expand result as the name.

There are two types of redirection standard output/Standard errors:

&>word

>&word

The first of the two forms is recommended. It is semantically equivalent to the following usage methods:

>word 2>&1


Append redirect standard output and standard error

This structure allows you to append standard output (file descriptor 1) and standard error (file descriptor 2) to the end of a file that has the word expand result as the name.

Additional standard output and standard error formats are:

&>>word

It is semantically equivalent to the following usage methods:

>>word 2>&1


Documents here

This redirection structure allows the shell to read input from the current source file until it encounters a line containing only word (and no trailing blanks). Until this point all lines are used as standard input for the command.

The format of the document here is:

<<[-]word

Here-document

Delimiter

Word does not have parameter expansion, command substitution, arithmetic expansion, or path expansion. If any character in Word is a reference, delimiter will be the result of a reference delete to Word, where the lines in the document are not expanded. If Word is not referenced, all lines in the document are expanded by parameters, command substitution, and arithmetic expansion. In the latter case, the character sequence "\<newline>" is ignored; "\" must be used to refer to the character "\", "$", and "'".

If the redirect operator is "<<-", all leading tab characters are removed from the input line and the row containing the delimiter. This allows the document in the shell script to be better indented.


The string here

Here is a variant of the document, in the form

<<<word

Word is expanded to provide commands as standard input.


Copy file descriptor

[N]<&word

The redirect operator is used to copy file descriptors. If Word expands to one or more numbers, n represents the file descriptor that will be the copy of that file descriptor. If a number in Word does not specify a file descriptor to be used for reading, a redirect error is generated. If Word expands to "-", the file descriptor n is closed. If n is not specified, standard input (file descriptor 0) is used.


[N]>&word

This operator is also used to copy output file descriptors. If n is not specified, standard output (file descriptor 1) is used. If a number in Word does not specify a file descriptor to be used for output, a redirect error is generated. In special cases, if n is skipped, and word is not expanded to one or more numbers, standard output and standard errors are redirected, as described earlier.


Move File descriptor

REDIRECT operator

[n]<&digit-

Move the file descriptor digit to file descriptor N, or standard input (file descriptor 0) If n is not specified. Digit is closed after it has been copied to N.

Similarly, the redirect operator

[n]>&digit-

Move the file descriptor digit to file descriptor N, or standard output (file descriptor 1) If n is not specified.


Open file descriptors for read and write operations

REDIRECT operator

[N]<>word

Causes the file to be opened with the word expand result as the name, read and write through the file descriptor N. If n is not specified, then the file descriptor 0 is used. If the file does not exist, it will be created.

This article is from the "home of the Ops" blog, so be sure to keep this source http://zhaotianyu.blog.51cto.com/132212/1786054

Basic analysis of Linux operating system (vii)--bash (Shell) Basics (4)

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.