Manipulating and/or expanding variables
${parameter}
Same as $parameter, i.e., value of the variable parameter. In certain contexts, only the less ambiguous ${parameter} form works.
${parameter-default}, ${parameter:-default}
If parameter not set, use default.
${parameter=default}, ${parameter:=default}
If parameter not set, set it to default.
${parameter+alt_value}, ${parameter:+alt_value}
If parameter set, use alt_value, else use null string.
${parameter?err_msg}, ${parameter:?err_msg}
If parameter set, use it, else print err_msg and abort the script with an exit status of 1.
Variable length / Substring removal
${#var}
String length (number of characters in $var). For an array, ${#array} is the length of the first element in the array.
${var#Pattern}, ${var##Pattern}
${var#Pattern} Remove from $var the shortest part of $Pattern that matches the front end of $var.
${var##Pattern} Remove from $var the longest part of $Pattern that matches the front end of $var.
${var%Pattern}, ${var%%Pattern}
${var%Pattern} Remove from $var the shortest part of $Pattern that matches the back end of $var.
${var%%Pattern} Remove from $var the longest part of $Pattern that matches the back end of $var.
Variable expansion / Substring replacement
These constructs have been adopted from ksh.
${var:pos}
Variable var expanded, starting from offset pos.
${var:pos:len}
Expansion to a max of len characters of variable var, from offset pos. See Example A-13 for an example of the creative use of this operator.
${var/Pattern/Replacement}
First match of Pattern, within var replaced with Replacement.
If Replacement is omitted, then the first match of Pattern is replaced by nothing, that is, deleted.
${var//Pattern/Replacement}
Global replacement. All matches of Pattern, within var replaced with Replacement.
As above, if Replacement is omitted, then all occurrences of Pattern are replaced by nothing, that is, deleted.
${var/#Pattern/Replacement}
If prefix of var matches Pattern, then substitute Replacement for Pattern.
${var/%Pattern/Replacement}
If suffix of var matches Pattern, then substitute Replacement for Pattern.
${!varprefix*}, ${!varprefix@}
Matches names of all previously declared variables beginning with varprefix.
${parameter}
Same as $parameter, i.e., value of the variable parameter. In certain contexts, only the less ambiguous ${parameter} form works.
${parameter-default}, ${parameter:-default}
If parameter not set, use default.
${parameter=default}, ${parameter:=default}
If parameter not set, set it to default.
${parameter+alt_value}, ${parameter:+alt_value}
If parameter set, use alt_value, else use null string.
${parameter?err_msg}, ${parameter:?err_msg}
If parameter set, use it, else print err_msg and abort the script with an exit status of 1.
Variable length / Substring removal
${#var}
String length (number of characters in $var). For an array, ${#array} is the length of the first element in the array.
${var#Pattern}, ${var##Pattern}
${var#Pattern} Remove from $var the shortest part of $Pattern that matches the front end of $var.
${var##Pattern} Remove from $var the longest part of $Pattern that matches the front end of $var.
${var%Pattern}, ${var%%Pattern}
${var%Pattern} Remove from $var the shortest part of $Pattern that matches the back end of $var.
${var%%Pattern} Remove from $var the longest part of $Pattern that matches the back end of $var.
Variable expansion / Substring replacement
These constructs have been adopted from ksh.
${var:pos}
Variable var expanded, starting from offset pos.
${var:pos:len}
Expansion to a max of len characters of variable var, from offset pos. See Example A-13 for an example of the creative use of this operator.
${var/Pattern/Replacement}
First match of Pattern, within var replaced with Replacement.
If Replacement is omitted, then the first match of Pattern is replaced by nothing, that is, deleted.
${var//Pattern/Replacement}
Global replacement. All matches of Pattern, within var replaced with Replacement.
As above, if Replacement is omitted, then all occurrences of Pattern are replaced by nothing, that is, deleted.
${var/#Pattern/Replacement}
If prefix of var matches Pattern, then substitute Replacement for Pattern.
${var/%Pattern/Replacement}
If suffix of var matches Pattern, then substitute Replacement for Pattern.
${!varprefix*}, ${!varprefix@}
Matches names of all previously declared variables beginning with varprefix.
Comments
Post a Comment
https://gengwg.blogspot.com/