wiki/help/linux/bash/data_manipulation/string.md

795 B

string operations.

case operations.

local myword="Hello"
echo ${myword^}  # first letter uppercase.
echo ${myword^^} # all uppercase.
echo ${myword,}  # first letter lowercase.
echo ${myword,,} # all lowercase.
echo ${myword~}  # reverses the case for the first letter.
echo ${myword~~} # reverses the case for every letter.

substitution.

local example="file.tar.gz"
echo "${example#file}" # ".tar.gz" - remove "file" from the start.
echo "${example%gz}"   # "file.tar." - remove "gz" from the end.
echo "${example#*.}"   # "tar.gz" - shortest from the start to dot.
echo "${example##*.}"  # "gz" - longest from the start to dot.
echo "${example%.*}"   # "file.tar" - shortest from the end to dot.
echo "${example%%.*}"  # "file" - longest from the end to dot.