Variables in bash
Read only variables
readonly someVariable=someValue
someVariable is assigned a value someValue. This value cannot be modified. An attempt to do this will result in an error:
line 5: someVariable: readonly variable
Variables scope
Reduce a variable scope to a function
Variable default value
Supposing, we need to implement the following requirement:
- if the
${HOST_NAME}environment has a non-empty value, assign it tohostvariable, - otherwise, assign
localhostvalue tohostvariable.
host={HOST_NAME:localhost}
Output a variable value with modified letters case
Output 'someVariable' value with the first letter capitalized
someVariable=somevalue
echo ${someVariable^}
Outputs:
Somevalue
Output 'someVariable' value with all letters capitalized
someVariable=someValue
echo ${someVariable^^}
Outputs:
SOMEVALUE
Output 'someVariable' value with the first letter in lower case
someVariable=SOMEvalue
echo ${someVariable,}
Outputs:
sOMEvalue
Output 'someVariable' value with all letters in lower case
someVariable=SOMEvalue
echo ${someVariable,,}
Outputs:
somevalue
Output a variable value with some parts dynamically replaced by other values
Output a variable value with all occurences of a substring replaced with some other string
Supposing, there exists a variable someVariable with value some variable value. We need to ouput the someVariable value with all whitespaces replaced by dashes:
someVariable="some variable value"
echo ${someVariable// /-}
Outputs:
some-variable-value
Output a variable value with only the first occurence of substring replaced with some other string
Supposing, there exists a variable someVariable with value project,module1,module2,module3. We need to ouput the someVariable value with the first comma replaced by a colon:
someVariable="project,module1,module2,module3"
echo ${someVariable/,/:}
Outputs:
project:module1,module2,module3
Output a variable value with a special character replaced by another character
Supposing, there exists a variable someVariable with value path/dir1/dir2/file. We need to ouput the someVariable value with the first slash replaced by a colon:
someVariable="path/dir1/dir2/file"
echo ${someVariable/\//:}
Outputs:
path:dir1/dir2/file
A backslash is used to escape a forward slash in the regular expression.
Get a slice of a variable value
Examples are based on the bash substring expansion:
${parameter:offset}
${parameter:offset:length}
The first element index is 0. As an example let's take all characters from the 4th till the last one:
s=200730
echo ${s:4}
Output: 30
In another example we will take two characters starting from the 2nd one:
s=200730
echo ${s:2:2}
Output: 07
Get a value of a variable which name is specified in another variable
Supposing, there exists a variable someVariable with value someValue. In case the someVariable is constructed dynamically in the script, and we need to get someValue by someVariable, we can use:
someVariable=someValue
firstPartOfVariableName=some
secondPartOfVariableName=Variable
variableName="${firstPartOfVariableName}${secondPartOfVariableName}"
echo ${!variableName}
Outputs:
someValue