Библиотека сайта rus-linux.net
10.3. Operations on variables
10.3.1. Arithmetic on variables
We discussed this already in Section 3.4.6.
10.3.2. Length of a variable
Using the ${#VAR} syntax will calculate the number of characters in a variable. If VAR is "*" or "@", this value is substituted with the number of positional parameters or number of elements in an array in general. This is demonstrated in the example below:
|
10.3.3. Transformations of variables
10.3.3.1. Substitution
${VAR:-WORD}
If VAR is not defined or null, the expansion of WORD is substituted; otherwise the value of VAR is substituted:
|
This form is often used in conditional tests, for instance in this one:
|
It is a shorter notation for
if |
See Section 7.1.2.3 for more information about this type of condition testing.
If the hyphen (-) is replaced with the equal sign (=), the value is assigned to the parameter if it does not exist:
|
The following syntax tests the existence of a variable. If it is not set, the expansion of WORD is printed to standard out and non-interactive shells quit. A demonstration:
|
Using "+" instead of the exclamation mark sets the variable to the expansion of WORD; if it does not exist, nothing happens.
10.3.3.2. Removing substrings
To strip a number of characters, equal to OFFSET, from a variable, use this syntax:
${VAR:OFFSET:LENGTH}
The LENGTH parameter defines how many characters to keep, starting from the first character after the offset point. If LENGTH is omitted, the remainder of the variable content is taken:
|
${VAR#WORD}
and
${VAR##WORD}
These constructs are used for deleting the pattern matching the expansion of WORD in VAR. WORD is expanded to produce a pattern just as in file name expansion. If the pattern matches the beginning of the expanded value of VAR, then the result of the expansion is the expanded value of VAR with the shortest matching pattern ("#") or the longest matching pattern (indicated with "##").
If VAR is * or @, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list.
If VAR is an array variable subscribed with "*" or "@", the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list. This is shown in the examples below:
|
The opposite effect is obtained using "%" and "%%", as in this example below. WORD should match a trailing portion of string:
|
10.3.3.3. Replacing parts of variable names
This is done using the
${VAR/PATTERN/STRING}
or
${VAR//PATTERN/STRING}
syntax. The first form replaces only the first match, the second replaces all matches of PATTERN with STRING:
|
More information can be found in the Bash info pages.
