Variable Substitution

variable=value
Set the variable’s value to value.
${variable}
Use the value of variable. The curly brackets are optional for a string variable separated from the surrounding code; they are always required for array variables.
${variable:-value}
If the variable is set, its value is used; otherwise the specified value is used.
${#variable}
Use the length of variable.
${variable#pattern}
Use the value of variable after removing characters matching pattern from the left. This removes the shortest matching piece.
${variable##pattern}
Use the value of variable after removing characters matching pattern from the left. This removes the longest matching piece.
${variable%pattern}
Use the value of variable after removing characters matching pattern from the right. This removes the shortest matching piece.
${variable%%pattern}
Use the value of variable after removing characters matching pattern from the right. This removes the longest matching piece.
${variable/pattern/replacement}
Use the value of variable with the first match of pattern replaced by replacement.
${variable//pattern/replacement}
Use the value of variable with every match of pattern replaced by replacement.
${variable/#pattern/replacement}
Use the value of variable with match of pattern replaced by replacement at the beginning of the value.
${variable/%pattern/replacement}
Use the value of variable with match of pattern replaced by replacement at the end of the value.

Examples

echo ${STR:-$(date +'%F')}
STR="/path/to/video.file.mkv"

echo ${STR##*/}            # video.file.mkv
echo ${STR##*.}            # mkv
echo ${STR%/*}             # /path/to
echo ${STR%.*}             # /path/to/video.file

echo ${STR%.mkv}           # /path/to/video.file
echo ${STR%.mkv}.mp4       # /path/to/video.file.mp4
echo ${STR/video./video_}  # /path/to/video_file.mkv
echo ${STR/./_}            # /path/to/video_file.mkv
echo ${STR//./_}           # /path/to/video_file_mkv
RED='\033[1;31m'   # red boldface
BLUE='\033[1;34m'  # blue boldface
NC='\033[0m'       # no colour, return to regular font

echo -e "${RED}Error!${NC} Bad news.\a"
echo -e "${BLUE}All okay.${NC} Good news."

2020–07–16