⇤ ← Revision 1 as of 2023-01-09 06:17:11
Size: 5658
Comment:
|
Size: 5713
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 56: | Line 56: |
'''Conditional parameter expansions''' provide default values if the parameter is unset or empty. | === Conditional Parameter Expansion === Conditional parameter expansions provide default values if the parameter is unset or empty. |
Line 100: | Line 104: |
'''Substring parameter expansion''' are string (and array) operations. | === Substring Parameter Expansion === Strings can be sliced in parameter expansion. |
Line 124: | Line 132: |
---- | |
Line 127: | Line 134: |
== Indirect Parameter Expansion == |
=== Indirect Parameter Expansion === |
Bash Expansion
Contents
Brace Expansion
{a,b} is expanded to the tokens a and b. This can be used like:
cp /path/to/file/example{,.bak} # or touch example.{c,h}
Sequence Expansion
{1..3} is expanded to the tokens 1, 2, and 3.
An increment can be optionally specified like {1..3..1}.
If the first number is larger than the second, then the token expands to a decreasing sequence. Behind the scenes, in this case, the increment defaulted to -1.
Tilde Expansion
The tilde (~) expands to a directory. Which directory depends on what, if any, characters follow it.
Tilde Prefix |
Expansion |
~ |
$HOME |
~/foo |
$HOME/foo |
~me/foo |
subdirectory foo within the home directory of user me |
~+/foo |
$PWD/foo |
~-/foo |
if $OLDPWD is set, $OLDPWD/foo |
~N |
for an integer N, the Nth element in the directory stack (dirs +N) |
~+N |
for an integer N, the Nth element in the directory stack (dirs +N) |
~-N |
for an integer N, the -Nth element in the directory stack (dirs -N) |
Parameter Expansion
The simplest form of parameter expansion is ${parameter}.
Conditional Parameter Expansion
Conditional parameter expansions provide default values if the parameter is unset or empty.
Suppose that:
export a=a export b= unset c
Operator |
Parameter Expansion |
Value |
Side Effects |
:- |
${a:-d} |
a |
|
:- |
${b:-d} |
d |
|
:- |
${c:-d} |
d |
|
- |
${a-d} |
a |
|
- |
${b-d} |
|
|
- |
${c-d} |
d |
|
:= |
${a:=d} |
a |
|
:= |
${b:=d} |
d |
b is set to d |
:= |
${c:=d} |
d |
c is set to d |
= |
${a=d} |
a |
|
= |
${b=d} |
|
|
= |
${c=d} |
d |
c is set to d |
:? |
${a:?d} |
a |
|
:? |
${b:?d} |
d |
shell exits |
:? |
${c:?d} |
d |
shell exits |
? |
${a?d} |
a |
|
? |
${b?d} |
|
|
? |
${c?d} |
d |
shell exits |
:+ |
${a:+d} |
d |
|
:+ |
${b:+d} |
|
|
:+ |
${c:+d} |
|
|
+ |
${a+d} |
d |
|
+ |
${b+d} |
d |
|
+ |
${c+d} |
|
|
The general rules are that:
including a colon (:) checks for unset and empty values; excluding it checks for only unset values
the subtraction sign (-) uses the left value value if possible, otherwise uses the right hand value
the equals sign (=) uses the left value value if possible, otherwise uses the right hand value and set the left hand value to be equal to the right hand value
the question mark (?) uses the left value value if possible, otherwise throws an error and exits the shell
the plus sign (+) uses the right value value if the left hand value is available, otherwise is empty
Substring Parameter Expansion
Strings can be sliced in parameter expansion.
string=01234567890abcdefgh echo ${string:7} # 7890abcdefgh echo ${string:7:2} # 78 echo ${string:7:-2} # 7890abcdef echo ${string: -7} # bcdefgh echo ${string: -7:-2} # bcdef
Arrays use the same syntax if they are subscripted with [@] or [*].
array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h) echo ${array[@]:7} # 7 8 9 0 a b c d e f g h
Indirect Parameter Expansion
If a parameter expansion begins with !, then the expansion is interpretted as input for another expansion.
export foo=bar export bar=baz echo ${foo} # bar echo ${!foo} # baz
Arithmetic Expansion
Command Expansion
Process Expansion
Word Splitting
Filename Expansion
Quoting
To avoid ambiguity with variable expansion, ${ is not eligible for these types of expansion.
{ and , may be quoted within brace expansion to ensure they are interpretted as literal characters.