Strings in terminal scripting are sequences of characters enclosed either in quotes or handled as text variables. Mastering string manipulation is essential for tasks like filename handling, messaging, or data parsing.
my_string="Hello World"
my_string='Hello $USER'
(no variable substitution)my_string="Hello $USER"
(variables are expanded)echo "Hello, World"
printf "%s\n" "$my_string"
${#my_string}
echo "Length: ${#my_string}"
${my_string:position}
(starts from position)${my_string:position:length}
(starts at position, length length)echo "${my_string:0:5}"
(outputs Hello
)${my_string/find/replace}
${my_string//find/replace}
echo "${my_string/World/Terminal}"
full_string="$string1$string2"
full_string="$string1 - $string2"
echo "$full_string"
[[ "$a" == "$b" ]]
[[ "$a" != "$b" ]]
[[ -z "$a" ]]
(zero length)[[ -n "$a" ]]
if [[ "$a" == "$b" ]]; then
echo "Strings are equal"
fi
string="apple,banana,cherry"
IFS=',' read -ra fruits <<< "$string"
echo "${fruits[0]}" # outputs apple
string=" Hello World "
trimmed=$(echo "$string" | xargs)
echo "$trimmed"
${string,,}
${string^^}
text="Hello"
echo "${text,,}" # hello
echo "${text^^}" # HELLO
${string#pattern}
(shortest match)${string##pattern}
${string%pattern}
${string%%pattern}
path="/home/user/file.txt"
echo "${path##*/}" # file.txt
echo "${path%/*}" # /home/user