Bash Variable Expansions
By Taha • 5 minutes read •
Bash (short for Bourne-Again SHell), is a shell program and command language supported by the Free Software Foundation and first developed for the GNU Project by Brian Fox. Designed as a 100% free software alternative for the Bourne shell, it was initially released in 1989. Its moniker is a play on words, referencing both its predecessor, the Bourne shell, and the concept of rebirth.
Today I will be explaining the variable expansions in bash. Variable expansions are a way to manipulate variables in bash. They are used to perform various operations on variables, such as extracting substrings, replacing text, and more.
${#variable}
This expansion returns the length of the variable. For example:
variable="Hello, World!"
Output:
${variable%pattern}
This expansion removes the match of the pattern from the end of the variable. Wildcards can be used in the pattern. For example:
variable="Hello world hello world 123!"
Output:
${variable#pattern}
This expansion removes the match of the pattern from the beginning of the variable. Wildcards can be used in the pattern. For example:
variable="Hello world hello world 123"
Output:
${variable%%pattern}
This expansion removes the longest match of the pattern from the end of the variable. Wildcards can be used in the pattern. For example:
variable="Hello world hello world 123!"
Output:
${variable##pattern}
This expansion removes the longest match of the pattern from the beginning of the variable. Wildcards can be used in the pattern. For example:
variable="Hello world hello world 123"
Output:
${variable^}
This expansion converts the first character of the variable to uppercase. For example:
variable="hello, world 123!"
Output:
${variable^^}
This expansion converts all characters of the variable to uppercase. For example:
variable="hello, world 123!"
Output:
${variable^^pattern}
This expansion converts all characters of the variable that match the pattern to uppercase. For example:
variable="hello, world 123!"
Output:
${variable,}
This expansion converts the first character of the variable to lowercase. For example:
variable="Hello, World 123!"
Output:
${variable,,}
This expansion converts all characters of the variable to lowercase. For example:
variable="Hello, World 123!"
Output:
${variable,,pattern}
This expansion converts all characters of the variable that match the pattern to lowercase. For example:
variable="Hello, World 123!"
Output:
${variable~}
This expansion swaps the case of the first character of the variable. For example:
variable="Hello, World 123!"
Output:
${variable~~}
This expansion swaps the case of all characters of the variable. For example:
variable="Hello, World 123!"
Output:
${variable~~pattern}
This expansion swaps the case of all characters of the variable that match the pattern. For example:
variable="Hello, World 123!"
Output:
${variable/pattern/replacement}
This expansion replaces the first match of the pattern with the replacement in the variable (like sed). For example:
variable="Hello, World 123!"
Output:
${variable//pattern/replacement}
This expansion replaces all matches of the pattern with the replacement in the variable (like sed). For example:
variable="Hello, World 123!"
Output:
${variable/%pattern/replacement}
This expansion replaces the last match of the pattern with the replacement in the variable (like sed). For example:
variable="Hello, World 123!"
Output:
${variable/#pattern/replacement}
This expansion replaces the first match of the pattern with the replacement in the variable (like sed). For example:
variable="Hello, World 123!"
Output:
${variable:position}
This expansion returns the substring of the variable starting from the position. For example:
variable="Hello, World 123!"
Output:
${variable:position:length}
This expansion returns the substring of the variable starting from the position with the specified length. For example:
variable="Hello, World 123!"
Output:
${variable: -position}
This expansion returns the substring of the variable starting from the position from the end. For example:
variable="Hello, World 123!"
Output:
${variable: -position:length}
This expansion returns the substring of the variable starting from the position from the end with the specified length. For example:
variable="Hello, World 123!"
Output:
${variable:+value}
This expansion returns the value if the variable is set and not null. For example:
variable="Hello, World 123!"
Output:
${variable:-value}
This expansion returns the value if the variable is not set or null. For example:
Output:
${variable:?message}
This expansion returns the message if the variable is not set or null. For example:
Output:
${variable:=value}
This expansion sets the value if the variable is not set or null. For example:
Output:
${!prefix*} and ${!prefix@}
This expansions returns the names of variables whose names start with the prefix. For example:
variable1="Hello" variable2="World"
Output:
${variable[*]} and ${variable[@]}
This expansions returns the values of the variable as an array. For example:
variable="Hello World 123!"
Output:
${parameter@operator}
This expansions performs various operations on the parameter. The operators are specified by the operator. For example:
U
- Uppercase the parameter.variable="hello, world 123!"
Output:
u
- Uppercase the first character of the parameter.variable="hello, world 123!"
Output:
L
- Lowercase the parameter.variable="HELLO, WORLD 123!"
Output:
Q
- Quote the parameter.variable="Hello, World 123!"
Output:
E
- Escape the parameter.variable="Hello, World 123!"
Output:
P
- Print the parameter.variable="Hello, World 123!"
Output:
A
- Assign the parameter.variable="Hello, World 123!"
Output:
variable='Hello, World 123!'
Conclusion
- Old servers may have different versions of commands like sed, grep, and awk. Bash variable expansion helps with this. These are the most common Bash variables. They help you work with variables in bash scripts. I hope this helps you use them. Thanks for reading!