LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 05:08 PM   #16
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled

Quote:
Originally Posted by rknichols View Post
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".
Every day a refresher course! Want being the keyword, I guess.
Thank you.
 
Old 02-08-2017, 12:50 AM   #17
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051Reputation: 6051
Quote:
Originally Posted by secomax View Post
Code:
\>
i'm really bothered by this.
why escape the '>' in the first place?
why doesn't it throw an error?
 
Old 02-08-2017, 01:05 AM   #18
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,453

Rep: Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062
Code:
[ "$val1" \> "$val2" ]
is a test command, and can be written as
Code:
test "$val1" \> "$val2"
Command arguments are subject to word splitting and expansion before execution (in the test command).
An unquoted > would be a redirection to a file, therefor the escape. You can also escape it with
Code:
test "$val1" ">" "$val2"
What counts is that the test sees 3 strings, where the 2nd it wants an operator.
This is different in the [[ ]] compound where the shell parser directly handles the operation without prior word splitting or expansion.
Code:
[[ $val1 > $val2 ]]
 
1 members found this post helpful.
Old 02-08-2017, 01:36 AM   #19
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Original Poster
Rep: Reputation: 0
Does that means I have to quote every instance of the string that contains spaces ?
 
Old 02-08-2017, 01:51 AM   #20
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,257

Rep: Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838
Quote:
Originally Posted by ondoho View Post
i'm really bothered by this.
why escape the '>' in the first place?
why doesn't it throw an error?
because syntactically correct, although it is just annoying and pointless.

Quote:
Does that means I have to quote every instance of the string that contains spaces ?
It is a really good idea, but there can be cases when you do not want to quote. So in general yes, but in special cases probably not.
 
Old 02-08-2017, 03:13 AM   #21
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Original Poster
Rep: Reputation: 0
If I remove the escape from it will give error
because the bash think it is an output redirection

Quote:
It is a really good idea, but there can be cases when you do not want to quote. So in general yes, but in special cases probably not.
Thanks for your help
I'm in love with bash scripts and bash tricks

Last edited by secomax; 02-08-2017 at 03:15 AM.
 
Old 02-08-2017, 03:23 AM   #22
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,257

Rep: Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838Reputation: 6838
Quote:
Originally Posted by secomax View Post
If I remove the escape from it will give error
Oh, yes, that is only working with [[ ]] (one more reason to use [[ ]] instead of [ ])
 
2 members found this post helpful.
Old 02-08-2017, 04:53 AM   #23
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Original Poster
Rep: Reputation: 0
Really thanks a lot
 
Old 02-08-2017, 07:05 AM   #24
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
another advantage to [[ ]] compared to [ ] is what if val1 or val2 were empty or null?

Code:
#cat strtest.sh
val1=
val2="somestring"

if [[ $val1 > $val2 ]]; then echo true; fi
#./strtest.sh
#
compared too

Code:
#cat strtest.sh
val1=
val2="somestring"

if [ $val1 \> $val2 ]; then echo true; fi
#./strtest.sh
./strtest.sh: line 4: [: >: unary operator expected
That is another behavioral difference to be aware of. This behavior will also be experienced if 'val1=""' too

Last edited by r3sistance; 02-08-2017 at 07:08 AM.
 
1 members found this post helpful.
Old 02-08-2017, 12:15 PM   #25
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,453

Rep: Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062
Empty variables are another reason for the rule:
always quote variables in command arguments, including the [ ] test command.
Literal strings that contain a space must always be in quotes.
Examples
Code:
var=* # assignment, does not need to be quoted.
var="* *" # literal string with whitespace, needs quotes

var="two words"
if [[ $var > "two words" ]]; then
  echo "$var comes after 'two words' in the alphabet"
fi

var="one two words"
case $var in
( *"two words"* )
  echo "'two words' found in '$var'."
;;
esac

if [[ $var == *"two words"* ]]; then
  echo "'two words' found in '$var'."
fi
One additional note: $var in 'ticks' is normally not expanded by the shell, but here the 'ticks' are within "quotes" so loose their special meaning.

Last edited by MadeInGermany; 02-08-2017 at 12:21 PM.
 
1 members found this post helpful.
Old 02-10-2017, 06:52 AM   #26
secomax
LQ Newbie
 
Registered: Feb 2017
Posts: 15

Original Poster
Rep: Reputation: 0
thanks for you all
it helped me
 
Old 02-10-2017, 07:57 AM   #27
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
You are welcome.

Go TeamLQ!
 
  


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 06:43 PM
[SOLVED] How to compare string 15.05 with float 15.0 in Linux shell script kverma1985 Programming 3 09-06-2012 02:30 AM
[SOLVED] bash regexp string compare stopped working jcrowley Programming 10 10-21-2010 12:03 PM
Compare $string from PHP script to /etc/shadow file Karas Linux - Newbie 5 11-27-2009 08:18 AM
Want to compare strings in bash script IsharaComix Programming 6 10-28-2008 08:49 PM

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

All times are GMT -5. The time now is 06:29 AM.

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
Open Source Consulting | Domain Registration