Bash Parameter and Variable Expansion: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 120: Line 120:
==Extract a Substring from a Certain Position==
==Extract a Substring from a Certain Position==


Specify the index of the first character of the substring. The substring is assumed to be extending to the end of the original string. Indices are 0-based:
Specify the index of the first character of the substring. The substring to be extracted is assumed to be extending to the end of the original string. Indices are 0-based.{{Note|There is no need to de-reference the variable that contains the index in ${s:i}, though ${s:${i}} will also work.}}
 
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
s="something"
s="something"
Line 127: Line 128:
</syntaxhighlight>
</syntaxhighlight>
produces "thing"
produces "thing"
{{Note|There is no need to de-reference the variable that contains the index in ${s:i}, though ${s:${i&#125;} will also work.}}

Revision as of 21:59, 18 September 2019

Internal

Overview

A dollar sign ($) that is NOT followed by an open parenthesis initiates parameter or variable expansion, which is a particular case of command line expansion. The simplest case of parameter expansion is retrieving the parameter value: the associated value is retrieved by prefixing the parameter's number, character or name with $ or enclosing it in ${...}.

Parameters and variables are not expanded if the string they are included in are enclosed within single quotes or if the leading dollar sign is escaped. Parameters and variables are expanded if the string they are included in is enclosed within double quotes.

a=10
echo "${a}"
echo '${a}'

produces

10
${a

Difference between $ and ${...}

TODO

Expand to a Default Value

http://wiki.bash-hackers.org/syntax/pe#use_a_default_value
${var:-alternative}

The :- form : if the variable 'var' referred in the expression was not defined, or it is the empty string, the expression expands the alternative.

${var-alternative}

The - form: if the colon is omitted, the expression is expanded to the alternative value only if the variable is not defined, not when it was empty.

Assign a Default Value

http://wiki.bash-hackers.org/syntax/pe#assign_a_default_value
 ${var:=word}
${var=word}

This expression works like expansion to a default value, but the word is not only expanded, but also assigned to the variable, if it was unset or null. The first form work if the variable is unset or an empty string, the second only if the parameter was unset.

Use an Alternate Value

http://wiki.bash-hackers.org/syntax/pe#use_an_alternate_value
${var:+alternative}

This form expands to nothing if the parameter is unset or empty.

${var+alternative}

This form expands to nothing if the variable is unset, and to the alternative if the variable is empty.

Variable Expansion in an Arbitrary File

If an arbitrary text file contains environment variable declarations, they can be expanded with envsubst. No other shell command line expansions will be performed. More details:

envsubst

String Length

The length of a variable's value:

${#var}

$!... Variable Indirection, Indirect Expansion

If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow immediately after the left brace in order to introduce indirection. Example:

a="b"
b="blah"
echo ${!a}

produces "blah".

Note that assigning a value to a indirect variable does not need to use "${!...}". For the above example, we can simply:

${a}=blah

This will assign "blah" to a variable named "b".

In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.

When not performing substring expansion, using the forms documented below, bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset.

Quoted String Expansion $'...'

This construct expands single or multiple escaped octal or hex values into ASCII or Unicode characters.

The following are equivalent:

$'\012' # Octal value
$'\x0a' # Hexadecimal value
$'\n'

Example:

echo "something"$'\047'"something else"$'\047'

The octal and hexadecimal values for ASCII characters are available here:

ASCII

Extract Substrings

Extract a Substring from a Certain Position

Specify the index of the first character of the substring. The substring to be extracted is assumed to be extending to the end of the original string. Indices are 0-based.


There is no need to de-reference the variable that contains the index in ${s:i}, though ${s:${i}} will also work.

s="something"
i=4
result=${s:i}

produces "thing"