Working with Strings
Strings are a fundamental part of any programming language, and Bash is no exception.
In this article, you will see some common string operations
String Concatenation
str1="Hello"
str2="World"
result="$str1 $str2"
echo $result # Output: Hello World
String Length
str="Hello"
length=${#str}
echo $length # Output: 5
Substring Extraction
str="Hello World"
substr=${str:6:5}
echo $substr # Output: World
String Replacement
str="Hello World"
new_str=${str/World/Bash}
echo $new_str # Output: Hello Bash
String Comparison
str1="Hello"
str2="World"
if [ "$str1" = "$str2" ]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi