This is where you will find information on how to work with bash strings

String Types

Using single or double quotes matter!

NAME="Stefan"
echo "Hi $NAME"  #=> Hi Stefan
echo 'Hi $NAME'  #=> Hi $NAME

Single quotes prevent you from using Bash Inline Functions ( $() syntax )

Get Length Of String

my_string="hello"
echo "${#my_string}"  # Output: 5

Substring Extraction

my_string="abcdefg"
echo "${my_string:2:3}"  # Output: cde

Removing Substrings

file_path="/home/user/document.txt"
echo "${file_path#*/}"   # Output: home/user/document.txt
file_path="/home/user/document.txt"
echo "${file_path##*/}"  # Output: document.txt
filename="document.txt"
echo "${filename%.txt}"  # Output: document
filename="archive.tar.gz"
echo "${filename%%.*}"   # Output: archive

Replacing Substrings

text="hello world, hello again"
echo "${text/hello/hi}"  # Output: hi world, hello again

Case Conversion

word="hello"
echo "${word^^}"  # Output: HELLO
word="WORLD"
echo "${word,,}"  # Output: world