LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-07-2017, 03:30 PM   #1
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Rep: Reputation: 0
bash script string compare


Hello
I'm new to Linux bash scripting
I was reading a tutorial about string comparison in bash scripts
I understand most of BUT one thing stops me.
the comparison of greater than syntax is very weird actually I don't understand it totally !!
I want a reasonable answer.
Why did he not put quotations on the text that contains spaces which are val2 while he puts the quotations on the if statements?
it actually works but not quite understandable !!!

Code:
#!/bin/bash
val1=text
val2=another text
if [ $val1 \> "$val2" ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
and this is the tutorial I get that snippet

https://likegeeks.com/bash-script-easy-guide/

I hope you help me get something I can understand
thanks in advance.
 
Old 02-07-2017, 04:32 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,362

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
Quote:
Originally Posted by secomax View Post
Why did he not put quotations on the text that contains spaces which are val2 while he puts the quotations on the if statements?
Code:
#!/bin/bash
val1=text
val2=another text
if [ $val1 \> "$val2" ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
If you are referring to the use of $val2 in the echo commands, it is already within a quoted string. Otherwise, I don't understand what you are questioning.
 
Old 02-07-2017, 04:35 PM   #3
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Original Poster
Rep: Reputation: 0
why the quotes is not on the third line like this
val2="another text"

instead of writing it within the if statement?
 
Old 02-07-2017, 04:47 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,549

Rep: Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842
Quote:
Originally Posted by secomax View Post
Why did he not put quotations on the text that contains spaces which are val2 while he puts the quotations on the if statements?
it actually works but not quite understandable !!!
Are you sure it works? It's giving an error for me:
Code:
$ ./str-compare.sh
./str-compare.sh: line 3: text: command not found
text is greater than
Quote:
Originally Posted by secomax
why the quotes is not on the third line like this
val2="another text"

instead of writing it within the if statement?
It's needed in both places. I guess he forgot it in the assignment line.
 
Old 02-07-2017, 04:49 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,631

Rep: Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074
that is a logical not
Code:
> greater than
< less than

\< not less than
\> not greater than
that is my thought. Even though it is not working on the command line.

ok maybe that is for strings only.
Code:
(userx@voider.org⚡️~)>>$ val1=text
(userx@voider.org⚡️~)>>$  
(userx@voider.org⚡️~)>>$ val2="another text"
(userx@voider.org⚡️~)>>$  
(userx@voider.org⚡️~)>>$ if [ $val1 \> "$val2" ]
>  
> then
>  
> echo "$val1 is greater than $val2"
>  
> else
>  
> echo "$val1 is less than $val2"
>  
> fi
text is greater than another text
which has more chars?

not greater \>

as text is not greater than another text it sends the first echo line, the response is written wrong is all.


Code:
 val1=text

val2="another text"

if [ val1 not greater than val2 ] then 

echo "$val1 is (not) greater than $val2"

else

echo "$val2 is greater than $val1"
fi
it should read the other way is all.

Last edited by BW-userx; 02-07-2017 at 05:09 PM.
 
Old 02-07-2017, 04:50 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,422
Blog Entries: 9

Rep: Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668Reputation: 3668
Quote:
Originally Posted by secomax View Post
the comparison of greater than syntax is very weird actually I don't understand it totally !!
neither do i, at least not for string variables.
i only ever use it inside the arithmetics (()) brackets:
Code:
if ((1 > 2))
then
   echo "Something is seriously wrong here!"
fi
 
Old 02-07-2017, 04:58 PM   #7
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Original Poster
Rep: Reputation: 0
Sorry my mistake
I forget the quotation
but this is not my question
I'm asking about the second quotation of the if statement
if [ $val1 \> "$val2" ]

why the quotation here?

Last edited by secomax; 02-07-2017 at 05:02 PM.
 
Old 02-07-2017, 05:04 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,549

Rep: Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842
Quote:
Originally Posted by secomax View Post
I'm asking about the second quotation of the if statement
if [ $val1 \> "$val2" ]

why the quotation here?
It's required to put quotes around $val2 because of word splitting. It would be better to also put quotes around $val1, even though in this example its value has no spaces so it won't be split.

Further reading:
http://mywiki.wooledge.org/WordSplitting
https://www.gnu.org/software/bash/ma...Splitting.html
 
Old 02-07-2017, 05:15 PM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,631

Rep: Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074
the person that posted that example on that other page made a boo boo is all.

Code:
val1="another text plus 2"
 
val2="another text"
 
if [ "$val1" \> "$val2" ]

then
 
echo "$val1 is greater than $val2"
 
else
 
echo "$val1 is less than $val2"
 
fi
returns
Code:
(userx@voider.org⚡️~)>>$ val1="another text plus 2"
(userx@voider.org⚡️~)>>$  
(userx@voider.org⚡️~)>>$ val2="another text"
(userx@voider.org⚡️~)>>$  
(userx@voider.org⚡️~)>>$ if [ "$val1" \> "$val2" ]
> 
> then
>  
> echo "$val1 is greater than $val2"
>  
> else
>  
> echo "$val1 is less than $val2"
>  
> fi
another text plus 2 is greater than another text

Last edited by BW-userx; 02-07-2017 at 05:17 PM.
 
Old 02-07-2017, 05:18 PM   #10
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Original Poster
Rep: Reputation: 0
I'm sorry
last question

when I type it like this

Code:
#!/bin/bash
val1=text
val2="another text"
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
it gives that error
./myscript: line 4: [: too many arguments

BUT when I type it as the tutorial like this

Code:
#!/bin/bash
val1=text
val2="another text"
if [ $val1 \> "$val2" ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
it works well
that's what I want to say from the beginning
 
Old 02-07-2017, 05:30 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,631

Rep: Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074Reputation: 2074
why use a not before the greater than is beyond me less than works because that is what it is actually doing

Code:
val1=1
 
val2=3
 
if [ $val1  \> $val2 ]

then
 
echo "$val1 is greater than $val2"
 
else
 
echo "$val1 is less than $val2"
 
fi
Code:
(userx@voider.org⚡️~)>>$ val1=1
(userx@voider.org⚡️~)>>$  
(userx@voider.org⚡️~)>>$ val2=3
(userx@voider.org⚡️~)>>$  
(userx@voider.org⚡️~)>>$ if [ $val1  \> $val2 ]
> 
> then
>  
> echo "$val1 is greater than $val2"
>  
> else
>  
> echo "$val1 is less than $val2"
>  
> fi
1 is less than 3
that string thing is screwy actually

m taking time to read everything (almost)
Quote:
One tricky note about the greater than and less than for string comparisons MUST be escaped with the back slash because by just using the greater-than symbol itself in the script, no errors are generated, but the results are wrong. The script interpreted the greater-than symbol as an output redirection. So you should do it like that

Last edited by BW-userx; 02-07-2017 at 05:34 PM.
 
Old 02-07-2017, 05:37 PM   #12
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
http://tldp.org/LDP/abs/html/comparison-ops.html

I've always used if [[ ... ]] for test constructs in bash.
Greater Than I believe is reserved for mathematical comparisons, Not strings?

Code:
man test && man bash
also
 
Old 02-07-2017, 05:44 PM   #13
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Original Poster
Rep: Reputation: 0
No, it is used for strings
I checked the documentation
although it work but I have no explanation !!! till now
 
Old 02-07-2017, 05:57 PM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,362

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
Quote:
Originally Posted by Habitual View Post
http://tldp.org/LDP/abs/html/comparison-ops.html

I've always used if [[ ... ]] for test constructs in bash.
Greater Than I believe is reserved for mathematical comparisons, Not strings? also
In both the "[ ... ]" and "[[ ... ]]" constructs in bash, the ">" and "<" operators perform a string comparison. If you want numeric comparison you have to use "-gt" and "-lt".
Code:
# String comparison sees that "1" is less than "9"
$ if [ 10 \> 9 ]; then echo greater; else echo not greater; fi
not greater
# Numeric comparison looks at the whole number
$ if [ 10 -gt 9 ]; then echo greater; else echo not greater; fi
greater

$ if [[ 10 > 9 ]]; then echo greater; else echo not greater; fi
not greater
$ if [[ 10 -gt 9 ]]; then echo greater; else echo not greater; fi
greater
Note that the "[[ ... ]]" construct parses its arguments differently, and the ">" must not be escaped if you want it to be recoginzed as an operator.

Another wrinkle is that in the "(( ... ))" construct, the ">" and "<" operators have their traditional, mathematical meaning.
Code:
$ if (( 10 > 9 )); then echo greater; else echo not greater; fi
greater
 
Old 02-07-2017, 05:59 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,549

Rep: Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842Reputation: 1842
Quote:
Originally Posted by secomax View Post
it gives that error
./myscript: line 4: [: too many arguments
That's because $val2 gets word split, like I said before.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script to compare months hilltownboy Linux - Newbie 5 03-23-2014 07:43 PM
[SOLVED] How to compare string 15.05 with float 15.0 in Linux shell script kverma1985 Programming 3 09-06-2012 03:30 AM
[SOLVED] bash regexp string compare stopped working jcrowley Programming 10 10-21-2010 01:03 PM
Compare $string from PHP script to /etc/shadow file Karas Linux - Newbie 5 11-27-2009 09:18 AM
Want to compare strings in bash script IsharaComix Programming 6 10-28-2008 09:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration