Most of the programmers who are new to shell scripting and dealing with string variables, have a doubt regarding when they have to wrap
quotes around a shell variable. In this article, we are going to perform some tests and try to understand when and where quotes around a
shell variable are required.
Table of Contents
List of scenarios when “quotes” are required
Sometimes when we are forgot to wrap a quotes around a shell variable it gives error or undesired output. most crucial condition is when
we are working with string having a space in it like “Good morning!”.
When we working with a string variable it’s a good habit to wrap it around quotes.
Scenario 1: Using length of string in an expression
Here we will perform a practical that find length of sting usind expr.
#declare a sting with space strValue="Hello world!" # expr command with two argument first is LENGTH # and second is string variable wrapped in quotes. length=`expr length "$strValue"` #print the length of string echo "Length of the string is : $length "
Output :
Length of the string is : 12
Now we qill try this above program without wrapping quotes around “$string” variable.
Frequently Asked:
- Linux: Find files larger than given size (gb/mb/kb/bytes)
- When to wrap quotes around a shell variable in Linux?
- Find Files containing specific Text in Linux
- Recursively Count Files in a directory in Linux
#declare a sting with space string="Hello world!" # expr command with two argument first is LENGTH # and second is string variable wraped in quotes. length=`expr length $string` #print the length of string echo "Length of the string is : $length "
Output :
expr: syntax error: unexpected argument ‘world!’ Length of the string is :
When we provided the second argument to the expr
without quotes
, then it is considered as multiple arguments. Like,
length=`expr length Hello world!`
And when we provide second argument to the expr
with quotes, the it considers the argument as one whole argument like,
length=`expr length "Hello world!"`
Scenario 2: Comparing string in bash script
Here, we will perform a practical example to compare two string, which have a space.
#read the first string from user echo "Enter first string:" read string1 #read the second string from user echo "Enter second string:" read string2 #compare two string if [ "$string1" = "$string2" ] then echo "Strings are equal." else echo "Strings are not equal." fi
Output :
Enter first string: red hat Enter second string: red hat Strings are equal.
If we compare two string varible without quote then it give undesired output. like,
#read the first string from user echo "Enter first string:" read string1 #read the second string from user echo "Enter second string:" read string2 #compare two string if [ $string1 = $string2 ] then echo "Strings are equal." else echo "Strings are not equal." fi
Output :
Enter first string: red hat Enter second string: red hat file1.sh: line 51: [: too many arguments Strings are not equal.
Here we got same type of error or warning as exapmple:1.
Scenario 3: Printing string array
In this practical, we will declare a string array and print it using for loop.
# declare the array arr=("tata nexon" "Toyota Fortuner" "swift" "Toyota Fortuner" "celerio") # print the array using for loop for list in "${arr[@]}"; do echo $list done
Output :
tata nexon Toyota Fortuner swift Toyota Fortuner celerio
Now we perform same above program with quote variable
# declare the array arr=("tata nexon" "Toyota Fortuner" "swift" "Toyota Fortuner" "celerio") # print the array using for loop for list in ${arr[@]}; do echo $list done
Output :
tata nexon Toyota Fortuner swift Toyota Fortuner celerio
You can see the difference between these two programs. It doesn’t give any error but gives the undesired output. As it takes “Toyota Fortuner” as a two seperate arguments.
Summary
In this article, we learned that when we are working with string with spaces, then we always wrap quotes around a shell variable because without quotes it takes the argument as multiple arguments and gives error or sometimes gives undesired output. Thanks.