A Pattern of Expansion
3.4. Beat expansion
three.4.1. General
After the command has been split into tokens (come across Section ane.4.i.i), these tokens or words are expanded or resolved. At that place are 8 kinds of expansion performed, which we will discuss in the next sections, in the lodge that they are expanded.
After all expansions, quote removal is performed.
iii.iv.2. Brace expansion
Brace expansion is a machinery past which capricious strings may be generated. Patterns to be brace-expanded take the form of an optional PREAMBLE, followed by a series of comma-separated strings betwixt a pair of braces, followed past an optional POSTSCRIPT. The preamble is prefixed to each cord contained within the braces, and the postscript is so appended to each resulting string, expanding left to right.
Brace expansions may exist nested. The results of each expanded string are not sorted; left to right order is preserved:
franky ~> echo sp{el,il,al}l spell spill spall
Brace expansion is performed before whatever other expansions, and whatever characters special to other expansions are preserved in the result. It is strictly textual. Bash does non use any syntactic interpretation to the context of the expansion or the text between the braces. To avert conflicts with parameter expansion, the string "${" is not considered eligible for brace expansion.
A correctly-formed brace expansion must comprise unquoted opening and closing braces, and at to the lowest degree one unquoted comma. Whatever incorrectly formed brace expansion is left unchanged.
3.4.iii. Tilde expansion
If a word begins with an unquoted tilde character ("~"), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix post-obit the tilde are treated as a possible login proper name. If this login name is the null cord, the tilde is replaced with the value of the HOME crush variable. If HOME is unset, the abode directory of the user executing the trounce is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login proper noun.
If the tilde-prefix is "~+", the value of the trounce variable PWD replaces the tilde-prefix. If the tilde-prefix is "~-", the value of the beat out variable OLDPWD, if it is prepare, is substituted.
If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed past a "+" or a "-", the tilde-prefix is replaced with the corresponding element from the directory stack, as it would be displayed by the dirs built-in invoked with the characters following tilde in the tilde-prefix equally an argument. If the tilde-prefix, without the tilde, consists of a number without a leading "+" or "-", "+" is assumed.
If the login proper noun is invalid, or the tilde expansion fails, the word is left unchanged.
Each variable assignment is checked for unquoted tilde-prefixes immediately post-obit a ":" or "=". In these cases, tilde expansion is also performed. Consequently, one may utilize file names with tildes in assignments to PATH, MAILPATH, and CDPATH, and the shell assigns the expanded value.
Example:
franky ~> export PATH= "$PATH:~/testdir"
~/testdir volition exist expanded to $Domicile /testdir, so if $Dwelling house is /var/home/franky, the directory /var/abode/franky/testdir will be added to the content of the PATH variable.
3.4.4. Shell parameter and variable expansion
The "$" graphic symbol introduces parameter expansion, control substitution, or arithmetics expansion. The parameter name or symbol to be expanded may exist enclosed in braces, which are optional merely serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.
When braces are used, the matching ending caryatid is the first "}" not escaped by a backslash or within a quoted string, and not within an embedded arithmetics expansion, control substitution, or parameter expansion.
The basic grade of parameter expansion is "${PARAMETER}". The value of "PARAMETER" is substituted. The braces are required when "PARAMETER" is a positional parameter with more than than one digit, or when "PARAMETER" is followed past a character that is not to be interpreted as function of its name.
If the first character of "PARAMETER" is an exclamation point, Fustigate uses the value of the variable formed from the residual of "PARAMETER" every bit the proper name of the variable; this variable is and so expanded and that value is used in the remainder of the substitution, rather than the value of "PARAMETER" itself. This is known as indirect expansion.
Yous are certainly familiar with straight parameter expansion, since it happens all the fourth dimension, even in the simplest of cases, such as the one above or the following:
franky ~> echo $SHELL /bin/bash
The post-obit is an example of indirect expansion:
franky ~> echo ${!Northward*} NNTPPORT NNTPSERVER NPX_PLUGIN_PATH
Note that this is not the aforementioned equally echo $N* .
The following construct allows for creation of the named variable if it does non nevertheless exist:
${VAR:= value }
Case:
franky ~> echo $FRANKY franky ~> repeat ${FRANKY:=Franky} Franky
Special parameters, among others the positional parameters, may not be assigned this fashion, however.
We volition further discuss the use of the curly braces for treatment of variables in Chapter 10. More information tin also be found in the Bash info pages.
3.4.5. Control exchange
Command substitution allows the output of a command to replace the control itself. Command substitution occurs when a command is enclosed like this:
$(command)
or like this using backticks:
`command`
Bash performs the expansion by executing Control and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may exist removed during give-and-take splitting.
franky ~> echo `engagement` Thu February 6 10:06:20 CET 2003
When the old-mode backquoted class of exchange is used, backslash retains its literal meaning except when followed past "$", "`", or "\". The first backticks not preceded by a backslash terminates the command substitution. When using the "$(COMMAND)" grade, all characters between the parentheses brand up the command; none are treated peculiarly.
Command substitutions may be nested. To nest when using the backquoted grade, escape the inner backticks with backslashes.
If the substitution appears within double quotes, word splitting and file proper name expansion are not performed on the results.
3.4.6. Arithmetic expansion
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the consequence. The format for arithmetic expansion is:
$(( EXPRESSION ))
The expression is treated equally if information technology were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter expansion, command substitution, and quote removal. Arithmetic substitutions may be nested.
Evaluation of arithmetic expressions is washed in stock-still-width integers with no check for overflow - although division by nothing is trapped and recognized as an mistake. The operators are roughly the same as in the C programming language. In order of decreasing precedence, the listing looks like this:
Table 3-4. Arithmetic operators
| Operator | Meaning |
|---|---|
| VAR++ and VAR-- | variable mail-increment and post-decrement |
| ++VAR and --VAR | variable pre-increment and pre-decrement |
| - and + | unary minus and plus |
| ! and ~ | logical and bitwise negation |
| ** | exponentiation |
| *, / and % | multiplication, division, residue |
| + and - | addition, subtraction |
| << and >> | left and correct bitwise shifts |
| <=, >=, < and > | comparison operators |
| == and != | equality and inequality |
| & | bitwise AND |
| ^ | bitwise exclusive OR |
| | | bitwise OR |
| && | logical AND |
| || | logical OR |
| expr ? expr : expr | conditional evaluation |
| =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^= and |= | assignments |
| , | separator between expressions |
Shell variables are allowed as operands; parameter expansion is performed earlier the expression is evaluated. Within an expression, shell variables may likewise be referenced by proper name without using the parameter expansion syntax. The value of a variable is evaluated every bit an arithmetic expression when it is referenced. A beat out variable demand not have its integer attribute turned on to be used in an expression.
Constants with a leading 0 (goose egg) are interpreted as octal numbers. A leading "0x" or "0X" denotes hexadecimal. Otherwise, numbers have the form "[BASE'#']N", where "Base" is a decimal number between 2 and 64 representing the arithmetics base of operations, and N is a number in that base of operations. If "BASE'#'" is omitted, and then base 10 is used. The digits greater than ix are represented by the lowercase letters, the uppercase messages, "@", and "_", in that order. If "Base of operations" is less than or equal to 36, lowercase and majuscule letters may exist used interchangably to represent numbers betwixt 10 and 35.
Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules to a higher place.
Wherever possible, Bash users should try to use the syntax with square brackets:
$[ EXPRESSION ]
Even so, this will only calculate the result of EXPRESSION, and do no tests:
franky ~> repeat $[365*24] 8760
Encounter Section 7.1.two.2, among others, for practical examples in scripts.
3.4.7. Process commutation
Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of
<(LIST)
or
>(LIST)
The process LIST is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the upshot of the expansion. If the ">(Listing)" form is used, writing to the file will provide input for List. If the "<(LIST)" form is used, the file passed every bit an argument should be read to obtain the output of LIST. Annotation that no space may appear between the < or > signs and the left parenthesis, otherwise the construct would be interpreted equally a redirection.
When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetics expansion.
More information in Section viii.two.iii.
3.4.eight. Word splitting
The vanquish scans the results of parameter expansion, command substitution, and arithmetic expansion that did non occur inside double quotes for word splitting.
The shell treats each character of $IFS equally a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, or its value is exactly "'<space><tab><newline>'", the default, then any sequence of IFS characters serves to circumscribe words. If IFS has a value other than the default, then sequences of the whitespace characters "space" and "Tab" are ignored at the beginning and end of the word, equally long as the whitespace character is in the value of IFS (an IFS whitespace graphic symbol). Whatsoever character in IFS that is not IFS whitespace, along with any adjacent IF whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is zip, no word splitting occurs.
Explicit nil arguments ("""" or "''") are retained. Unquoted implicit aught arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained.
| Expansion and give-and-take splitting |
|---|---|
| If no expansion occurs, no splitting is performed. |
iii.four.ix. File proper name expansion
After discussion splitting, unless the -f option has been set (see Department two.3.ii), Bash scans each word for the characters "*", "?", and "[". If ane of these characters appears, then the word is regarded every bit a Pattern, and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found, and the shell option nullglob is disabled, the give-and-take is left unchanged. If the nullglob choice is set, and no matches are plant, the discussion is removed. If the shell choice nocaseglob is enabled, the match is performed without regard to the example of alphabetic characters.
When a pattern is used for file name generation, the character "." at the start of a file name or immediately following a slash must be matched explicitly, unless the beat option dotglob is set. When matching a file name, the slash character must always be matched explicitly. In other cases, the "." character is not treated specially.
The GLOBIGNORE shell variable may be used to restrict the set of file names matching a pattern. If GLOBIGNORE is ready, each matching file name that too matches one of the patterns in GLOBIGNORE is removed from the list of matches. The file names . and .. are always ignored, even when GLOBIGNORE is set. Nevertheless, setting GLOBIGNORE has the effect of enabling the dotglob shell selection, so all other file names beginning with a "." will match. To get the quondam behavior of ignoring file names beginning with a ".", make ".*" one of the patterns in GLOBIGNORE. The dotglob option is disabled when GLOBIGNORE is unset.
porterhiscitifted.blogspot.com
Source: https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html
Belum ada Komentar untuk "A Pattern of Expansion"
Posting Komentar