LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-09-2018, 06:55 AM   #1
marcoubuntu
LQ Newbie
 
Registered: Aug 2017
Posts: 19

Rep: Reputation: Disabled
Bash integer comparision


I really can't understand why if I do integer comparision like this:

$ [ 1 == 1]

or

$ [ 1 -eq 1 ]

then

echo $?

I get 0 that is false instead of 1 that is true!

What I'm missing?
 
Old 04-09-2018, 07:01 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
$? returns the return value of the previous command (0 means success; any other value would indicate some sort of error).
 
Old 04-09-2018, 07:06 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
you are mixing your comparison operators in case you didn't know.
http://tldp.org/LDP/abs/html/comparison-ops.html
and, your understanding of what 0 and 1 indicate is backwards.

to keep it simple
0 = false, no errors were encountered.
1 = true, errors were encountered.
 
Old 04-09-2018, 02:38 PM   #4
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Rep: Reputation: 220Reputation: 220Reputation: 220
Quote:
Originally Posted by marcoubuntu View Post
I really can't understand why if I do integer comparision like this:

$ [ 1 == 1]
It should be:

Code:
[ 1 == 1 ]
Quote:
Originally Posted by marcoubuntu View Post
or

$ [ 1 -eq 1 ]

then

echo $?

I get 0 that is false instead of 1 that is true!

What I'm missing?
See what man bash says:

Code:
EXIT STATUS   
The exit status of an executed command is the value returned by the
waitpid system call or equivalent function.  Exit statuses fall
between 0 and 255, though, as explained below, the shell may
use values above 125 spe- cially.  Exit statuses from shell
builtins and compound commands are also limited to this
range. Under certain circumstances, the shell will use special
values to indicate specific failure modes.

For the shell's purposes, a command which exits with a zero exit
status has succeeded.  An exit status of zero indicates success.  A
non-zero exit status indicates failure.  When a command terminates on
a fatal signal N, bash uses the value of 128+N as the exit status.

Last edited by average_user; 04-09-2018 at 02:40 PM.
 
Old 04-09-2018, 03:51 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
In fact the shell has inverse logic for practical reason.
Code:
true; echo $?
false; echo $?
 
Old 04-09-2018, 04:48 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Exit status is an integer value returned by a function, not true or false value from operator

If you want true or false value
Code:
(( x = 1 == 1 ))
echo $x
 
Old 04-11-2018, 03:57 AM   #7
marcoubuntu
LQ Newbie
 
Registered: Aug 2017
Posts: 19

Original Poster
Rep: Reputation: Disabled
ok I understood, so I can even do:

Code:
x=$(expr 1 == 1 )
echo $x
or

Code:
INPUT='string1'
LABEL=$(expr "${INPUT}" == "string1")
echo "$LABEL"
result
Code:
1
Thank you

Last edited by marcoubuntu; 04-11-2018 at 03:59 AM.
 
Old 04-11-2018, 06:45 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes, but with expr you use an external program which is not necessary for integer comparision

Code:
x=$(( 1 == 1 ))
echo $x

Last edited by keefaz; 04-11-2018 at 06:49 AM.
 
Old 04-11-2018, 12:01 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
$(( )) is for arithmetics i.e. numbers only.
While expr takes strings.

With strings you can use the internal [ ] command or the more robust [[ ]] compound, and set the return value explicitly:
Code:
INPUT='string1'
if [[ $INPUT == "string1" ]]; then LABEL=1; else LABEL=0; fi
echo "$LABEL"
 
Old 04-11-2018, 03:13 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You can also write a function that inverts $? value
Code:
error_test() {
  (( $? != 0 ))
}

INPUT='string1'
[[ $INPUT == "string1" ]]; error_test
echo $?
But I wouldn't use this
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Test for integer in BASH minil Programming 18 02-01-2012 12:45 AM
Converting string to integer in BASH pgb205 Programming 17 08-31-2010 08:38 PM
bash scripting and integer variables. stf92 Linux - Software 1 06-06-2010 06:30 PM
Convert float to integer in bash gn00kie Programming 2 07-30-2006 10:05 PM
bash integer handling question mikee78 Programming 11 11-14-2005 04:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:50 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