bash.Variables
Bash parameter expansion is a powerful feature that allows you to manipulate and retrieve the value of variables in a variety of ways. Here’s a detailed explanation of some common and advanced forms of parameter expansion:
1. Default Values
-
${VARIABLE:-default}
: IfVARIABLE
is unset or null, it substitutesdefault
. Otherwise, it returns the value ofVARIABLE
.myvar= echo "${myvar:-Hello}" # Outputs "Hello" because myvar is empty
-
${VARIABLE:=default}
: IfVARIABLE
is unset or null, it sets it todefault
and returns the value.echo "${myvar:=Hello}" # myvar is set to "Hello"
-
${VARIABLE:+value}
: IfVARIABLE
is set and not null, it returnsvalue
. Otherwise, it returns nothing.myvar="world" echo "${myvar:+Hello}" # Outputs "Hello"
-
${VARIABLE:?message}
: IfVARIABLE
is unset or null, it prints an error message (message
) to stderr and exits the script. If no message is provided, a default message is shown.echo "${myvar:?myvar is not set}" # Prints an error if myvar is not set
2. Substring Operations
-
${VARIABLE:offset}
: Extracts a substring starting fromoffset
. Ifoffset
is negative, it starts from the end of the string.myvar="abcdef" echo "${myvar:2}" # Outputs "cdef" (starts from index 2)
-
${VARIABLE:offset:length}
: Extracts a substring starting fromoffset
with the specifiedlength
.echo "${myvar:1:3}" # Outputs "bcd" (3 characters starting from index 1)
3. String Length
-
${#VARIABLE}
: Returns the length of the string stored inVARIABLE
.echo "${#myvar}" # Outputs "6" (length of "abcdef")
4. Pattern Substitution
-
${VARIABLE#pattern}
: Removes the shortest match ofpattern
from the start ofVARIABLE
.myvar="file.txt" echo "${myvar#*.}" # Outputs "txt" (removes "file.")
-
${VARIABLE##pattern}
: Removes the longest match ofpattern
from the start.myvar="path/to/file.txt" echo "${myvar##*/}" # Outputs "file.txt" (removes "path/to/")
-
${VARIABLE%pattern}
: Removes the shortest match ofpattern
from the end ofVARIABLE
.echo "${myvar%.txt}" # Outputs "file" (removes ".txt")
-
${VARIABLE%%pattern}
: Removes the longest match ofpattern
from the end.myvar="path/to/file.txt" echo "${myvar%%/*}" # Outputs "path" (removes everything after the first "/")
5. Search and Replace
-
${VARIABLE/pattern/replacement}
: Replaces the first match ofpattern
withreplacement
.myvar="apple, orange, apple" echo "${myvar/apple/grape}" # Outputs "grape, orange, apple"
-
${VARIABLE//pattern/replacement}
: Replaces all matches ofpattern
withreplacement
.echo "${myvar//apple/grape}" # Outputs "grape, orange, grape"
-
${VARIABLE/#pattern/replacement}
: Ifpattern
matches the beginning ofVARIABLE
, replace it withreplacement
.echo "${myvar/#apple/grape}" # Replaces only if "apple" is at the start
-
${VARIABLE/%pattern/replacement}
: Ifpattern
matches the end ofVARIABLE
, replace it withreplacement
.echo "${myvar/%apple/grape}" # Replaces only if "apple" is at the end
6. Remove Variable Content
-
${VARIABLE@operator}
: An advanced feature that applies transformations based on the operator. Common ones are:@Q
: Quote the value (useful for shell-safe strings).@A
: Convert to an array.
myvar="Hello" echo "${myvar@Q}" # Outputs "'Hello'" (quoted)
Practical Examples
1. Setting Default Values
user="${USER:-defaultuser}"
2. Check if a Variable is Empty
: "${myvar:?myvar is required but not set}"
3. String Manipulation
- Remove file extension:
filename="document.txt"
echo "${filename%.txt}"
- Get the last part of a path:
path="/usr/local/bin"
echo "${path##\*/}" # Outputs "bin"