Bash String Manipulation

Advanced string operations

String Length

${#string} # length of string

Substring

${string:position} # substring from position to end
${string:position:length} # substring with length
${string: -5} # last 5 characters

Replace

${string/pattern/replacement} # replace first match
${string//pattern/replacement} # replace all matches

Remove Pattern

${string#pattern} # remove shortest match from start
${string##pattern} # remove longest match from start
${string%pattern} # remove shortest match from end
${string%%pattern} # remove longest match from end

Case Conversion

${string^} # uppercase first character
${string^^} # uppercase all
${string,} # lowercase first character
${string,,} # lowercase all

Concatenation

str1$str2 # concatenate strings
${str1}${str2} # concatenate (preferred)