$text="hello"$ext="txt"str_concat="$text.$ext"str_concat2="$text .$ext"# lesson: always use [[ ]] with string variables to avoid runtime errorsif [[ $str_concat = "hello .txt" ]]; thenecho"preferred method"fi# you can also use double quotes, but it's not as clean imoif [ "$str_concat" = "hello .txt" ]; thenecho"option B, use double quotes"fi# this will result in too many arguments error# if [ $str_concat2 = "hello .txt" ]; then# echo "not possible"# fi# this works only if the variable does not contain spacesif [ $str_concat = "hello.txt" ]; thenecho"works"fi
Loops and More Conditionals
# for loop examplesfor var in 123 str1 str2 str3 $flag19 10; doecho$vardone# rangefor var in {1..10}; doecho$vardone# for loop as while loop# some math here toocounter=0while [ $counter -lt 10 ]; doif [ $counter -eq 0 ]; then ((counter++))
echo"counter+2="$((++counter))elif [ $counter -lt 5 ]; thenecho"less than 5"elif [ $counter -gt 5 ]; thenecho"greater than 5"fidone