Results 1 to 4 of 4
Hello,
I want to check for a string in if condition
string = "a b c d"
if [ $string == "a b c d" ];then
echo MATCH
else
echo ...
- 04-25-2011 #1Just Joined!
- Join Date
- Jun 2010
- Posts
- 9
String Comparision
Hello,
I want to check for a string in if condition
string = "a b c d"
if [ $string == "a b c d" ];then
echo MATCH
else
echo DIFFERENT
fi
Its giving an error in if statement ( too many arguements)
Do I need to use regex for that comparison as string also have spaces between them?
- 04-25-2011 #2Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
The problem that you are seeing is that the value of "string" is expanded in the statement before it is evaluated. In your case the statement would be (assuming string is "a b c d"):
And as you see it is not a string to string comparison. To allow it to work do either of the following:Code:if [ a b c d == "a b c d" ] ; then
orCode:if [ "$string" == "a b c d" ] ; then
Code:if [ "${string}" == "a b c d" ] ; then
- 04-25-2011 #3
- 04-26-2011 #4Just Joined!
- Join Date
- Jun 2010
- Posts
- 9
Thanks..It Worked..


Reply With Quote
