Bash Command Line Expansion: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(47 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* http://www.informit.com/articles/article.aspx?p=441605&seqNum=9
* Parameter expansion http://wiki.bash-hackers.org/syntax/pe
=Internal=
=Internal=


Line 4: Line 9:


=Overview=
=Overview=
=Process=


Each line read by bash from command line is interpreted as a [[Bash_Concepts#Pipeline|pipeline]]: a sequence of commands separated by the [[Bash_Concepts#.7C|pipe character (|)]].
Each line read by bash from command line is interpreted as a [[Bash_Concepts#Pipeline|pipeline]]: a sequence of commands separated by the [[Bash_Concepts#.7C|pipe character (|)]].


Each command is split into tokens that separated by [[Bash_Concepts#Metacharacters|metacharacters]].
Command processing starts by checking the first word of the command against an alias list, and if a match is found, the alias is replaced.
 
Then the result is parsed into tokens by using [[Bash_Concepts#Metacharacters|metacharacters]] as separators.
 
==Command Line Expansion==
 
The token are scanned to perform ''command line expansion'', which are the following operations, performed in order:
 
===Brace Expansion===
 
Brace expansion is the process of expanding a set of ''comma-separated'' strings specified inside the braces into a ''space-separated'' list of strings. Each string from the list is prepended with a ''preamble'', and appended with the ''postscript''. Both the preamble and the postscript are optional. The left-to-right order of the strings within the braces is preserved in the expansion. Brace expansion occurs only if at least one comma exists inside the braces. Brace expansions can be nested.
 
echo KA{BO,DO}OM
KABOOM KADOOM
 
"KA" is the preamble and "OM" is the postscript.
 
Brace expansion is turned on in interactive and noninteractive shells by default. It can be turned off with:
 
set +o braceexpand
 
===Tilde Expansion===
 
The tilde (~) is a special character when it appears at the start of a command line token. When detected on the first position, bash looks at the string of character up to the first slash or to the end of the word, if there is no slash, as a possible login name.
 
If this possible login name is null - the tilde appears as a word by itself or if it is immediately followed by a slash - bash substitutes the value of the HOME variable for the tilde. If the login name is valid and not null, bash substitutes the path of the home directory associated with that login name. If the login name is not null, but not valid, no substitution is made.
 
===Parameter and Variable Expansion===
 
{{Internal|Bash Parameter and Variable Expansion|bash Parameter and Variable Expansion}}
 
===Arithmetic Expansion===
 
Bash performs arithmetic expansion by evaluating an arithmetic expression enclosed within (( and )) and replacing it with the result.
 
i=10
((i++))
echo ${i}
 
$(( )) and (( )) are equivalent syntax.
 
The rules for forming expression are the same as in C. those found in the C programming language; all standard C arithmetic operators are available. Arithmetic is done using integers. Strings are converted to integers. The variables need not be preceded with the $ sign, though the expression evaluates correctly if specified.
 
===<span id='Command_Substitution'></span>Command Substitution $(...)===
{{Internal|Bash Command Substitution|Command Substitution}}
 
===Word Splitting===
 
The results of [[#Parameter_and_Variable_Expansion|parameter and variable expansion]], [[#Command_Substitution|command substitution]], and [[#Arithmetic_Expansion|arithmetic expansion]] are candidates for word splitting: using each character of IFS as a possible delimiter, bash splits these candidates into words or tokens. If IFS is unset, bash uses its default value (space, tab and newline). If IFS is null, bash does not split words.
 
===<span id='Pathname_Expansion'></span><span id='Globbing'></span><span id='Glob'></span>Pathname Expansion (Filename Expansion, Filename Generation, Globbing)===
 
'''Pathname expansion''' or '''filename expansion''' is also called '''filename generation''' or '''globbing''', and it is the process of interpreting '''ambiguous file references''' and substituting the appropriate list of filenames.  Filename expansion does not use standard regular expressions. Instead, it recognizes and expands '''wildcards'''. An ambiguous file reference that uses wildcards is a token containing one of the unquoted [[Regular Expressions Concepts#Metacharacters|metacharacters]]: [[#The_Wild_Card_.28.2A.29|*]], [[#The_Question_Mark_.28.3F.29|?]], [[#The_Character_Class_.5B....5D|[ or ]]] and [[#Negation_.28.5E.29|^]] (for more details on individual wildcards, see below).
 
A period that either starts a pathname or follows a slash (/) in a pathname must be matched explicitly unless you have set "dot glob".
 
If the pathname expansion is unsuccessful - does not produce any hit - the original expression, including the special pathname expansion characters, are passed to the command.
 
Pathname expansion can be turned off with:
<syntaxhighlight lang='bash'>
noglob
</syntaxhighlight>
 
====The Wild Card (*)====
 
The wild card (<tt>*</tt>) matches every name in a given directory. Strings containing * will not match filenames that start with a dot.
 
====The Question Mark (?)====
 
The question mark (<tt>?</tt>) serves as a single-character "wild card" for filename expansion.
 
====The Character Class [...]====
 
The character class matches just one of the characters listed among square brackets. One of those characters must exist in the file name, on the specified position, for the name to match.
 
====Negation (^)====
 
<tt>^</tt> negates the sense of match for the above.
 
===Process Substitution===
 
==Quote Removal==
 
After all command line expansion specified above are performed, bash proceeds with ''quote removal'': it removes from the command line single quotation marks, double quotation marks and backslashes that are not a result of an expansion.

Latest revision as of 09:26, 4 July 2021

External

Internal

Overview

Process

Each line read by bash from command line is interpreted as a pipeline: a sequence of commands separated by the pipe character (|).

Command processing starts by checking the first word of the command against an alias list, and if a match is found, the alias is replaced.

Then the result is parsed into tokens by using metacharacters as separators.

Command Line Expansion

The token are scanned to perform command line expansion, which are the following operations, performed in order:

Brace Expansion

Brace expansion is the process of expanding a set of comma-separated strings specified inside the braces into a space-separated list of strings. Each string from the list is prepended with a preamble, and appended with the postscript. Both the preamble and the postscript are optional. The left-to-right order of the strings within the braces is preserved in the expansion. Brace expansion occurs only if at least one comma exists inside the braces. Brace expansions can be nested.

echo KA{BO,DO}OM
KABOOM KADOOM

"KA" is the preamble and "OM" is the postscript.

Brace expansion is turned on in interactive and noninteractive shells by default. It can be turned off with:

set +o braceexpand

Tilde Expansion

The tilde (~) is a special character when it appears at the start of a command line token. When detected on the first position, bash looks at the string of character up to the first slash or to the end of the word, if there is no slash, as a possible login name.

If this possible login name is null - the tilde appears as a word by itself or if it is immediately followed by a slash - bash substitutes the value of the HOME variable for the tilde. If the login name is valid and not null, bash substitutes the path of the home directory associated with that login name. If the login name is not null, but not valid, no substitution is made.

Parameter and Variable Expansion

bash Parameter and Variable Expansion

Arithmetic Expansion

Bash performs arithmetic expansion by evaluating an arithmetic expression enclosed within (( and )) and replacing it with the result.

i=10
((i++))
echo ${i}

$(( )) and (( )) are equivalent syntax.

The rules for forming expression are the same as in C. those found in the C programming language; all standard C arithmetic operators are available. Arithmetic is done using integers. Strings are converted to integers. The variables need not be preceded with the $ sign, though the expression evaluates correctly if specified.

Command Substitution $(...)

Command Substitution

Word Splitting

The results of parameter and variable expansion, command substitution, and arithmetic expansion are candidates for word splitting: using each character of IFS as a possible delimiter, bash splits these candidates into words or tokens. If IFS is unset, bash uses its default value (space, tab and newline). If IFS is null, bash does not split words.

Pathname Expansion (Filename Expansion, Filename Generation, Globbing)

Pathname expansion or filename expansion is also called filename generation or globbing, and it is the process of interpreting ambiguous file references and substituting the appropriate list of filenames. Filename expansion does not use standard regular expressions. Instead, it recognizes and expands wildcards. An ambiguous file reference that uses wildcards is a token containing one of the unquoted metacharacters: *, ?, [ or ] and ^ (for more details on individual wildcards, see below).

A period that either starts a pathname or follows a slash (/) in a pathname must be matched explicitly unless you have set "dot glob".

If the pathname expansion is unsuccessful - does not produce any hit - the original expression, including the special pathname expansion characters, are passed to the command.

Pathname expansion can be turned off with:

noglob

The Wild Card (*)

The wild card (*) matches every name in a given directory. Strings containing * will not match filenames that start with a dot.

The Question Mark (?)

The question mark (?) serves as a single-character "wild card" for filename expansion.

The Character Class [...]

The character class matches just one of the characters listed among square brackets. One of those characters must exist in the file name, on the specified position, for the name to match.

Negation (^)

^ negates the sense of match for the above.

Process Substitution

Quote Removal

After all command line expansion specified above are performed, bash proceeds with quote removal: it removes from the command line single quotation marks, double quotation marks and backslashes that are not a result of an expansion.