bash
This demonstrates prefix name expansion in Bash to list all variables that start with a specified prefix.
prefix_a=one prefix_b=two echo ${!prefix_*} # all variables names starting with `prefix_` # => prefix_a prefix_b
bash internaldata manipulationsstring manipulation and expansionsparameter expansionprefix expansion
bash
This demonstrates case conversion using parameter expansion in Bash.
str="HELLO WORLD!" echo "${str,}" #=> "hELLO WORLD!" (lowercase 1st letter) echo "${str,,}" #=> "hello world!" (all lowercase) str="hello world!" echo "${str^}" #=> "Hello world!" (uppercase 1st letter) echo "${str^^}" #=> "HELLO WORLD!" (all uppercase)
bash internaldata manipulationsstring manipulation and expansionsparameter expansioncase conversion
bash
This demonstrates substring extraction from a variable using parameter expansion.
length=7 echo "${variable:0:length}" # => Some st # This will return only the first 7 characters of the value echo "${variable: -5}" # => tring # This will return the last 5 characters (note the space before -5). # The space before minus is mandatory here.
bash internaldata manipulationsstring manipulation and expansionsparameter expansionstring slicing
bash
This demonstrates file extension extraction and manipulation using parameter expansion.
file="Some/path/to/file.txt" echo "Extension: ${file##*.}" echo "Name sans extension: ${file%.*}" echo "New extension: ${file%txt}gif"
bash internaldata manipulationsstring manipulation and expansionsparameter expansionpath manipulation