Timeline for How can I declare and use Boolean variables in a shell script?
Current License: CC BY-SA 4.0
19 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 22, 2018 at 13:06 | history | edited | Peter Mortensen | CC BY-SA 4.0 |
Added a cross reference. Active reading [<http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29>]. Changed to ISO 8601 date.
|
Jul 24, 2017 at 19:58 | comment | added | Mike Holt |
To address SeldomNeedy's comment, yes, you can use the real true , but generally not as something to compare a variable against, since the real true has no value per se. All it does is set the exit status to 0 , indicating success. It's worth noting that it's essentially equivalent to the so-called "null command", or : . As far as using 0 and 1 , that's what I do in all my scripts these days where I need booleans. And I use the (( )) operator instead of [[ ]] to evaluate. So, for example, if I have flag=0 , I can then do if (( flag )); then ...
|
|
Jul 24, 2017 at 19:31 | comment | added | phil294 |
wouldnt it make more sense to use 0 and 1 instead of false and true then? aka if [[ $a == 0 ]]; then ..
|
|
Jul 23, 2015 at 23:59 | comment | added | Seldom 'Where's Monica' Needy |
From reading the answers offered here, I get the impression that there's no such thing as actually using the real true . Is there a way? I suspect many programmers who are used to stricter languages viewing this answer to assist them in mixing up some bash glue to make their lives a bit easier would want an === operator so that strings and "booleans" aren't actually interchangeable. Should they just stick to 0 and 1 and use (( $maybeIAmTrue )) as suggested in Quolonel Question's answer?
|
|
Jun 8, 2015 at 23:49 | comment | added | Mike Holt | @wisbucky Thanks for that. I hadn't realized that there was a prior version of miku's answer, although I did note in my update on 2/19/2014 that the article linked to in miku's answer was doing something different. | |
Jun 8, 2015 at 23:10 | comment | added | wisbucky |
The reason for the confusion is that miku's original answer stood for 4 years. All the references to the builtin true were made regarding the original answer. (The revised answer on Feb 12, 2014 was not submitted by miku.) I have edited the answer to include both original and revised. Then people's comments make sense.
|
|
Apr 30, 2014 at 16:53 | history | edited | Mike Holt | CC BY-SA 3.0 |
deleted 1 character in body
|
Apr 30, 2014 at 14:56 | comment | added | Mike Holt |
@chepner You're right. I hate == and never use it in my own scripts. Not sure why I had that in there. Maybe just too much C on the brain.
|
|
Apr 30, 2014 at 12:32 | comment | added | chepner |
Using == with [ /test is not portable; if you want to use == , use [[ . If you want to use [ , use = .
|
|
Apr 14, 2014 at 17:09 | history | edited | Mike Holt | CC BY-SA 3.0 |
added 993 characters in body
|
Apr 14, 2014 at 17:01 | history | edited | Mike Holt | CC BY-SA 3.0 |
added 993 characters in body
|
Feb 20, 2014 at 1:19 | comment | added | Mike Holt | True. Although, I'm not advocating for (or against) either approach. I just wanted to clear up some of the misinformation that's getting voted up here, so that people who stumble upon this topic later on won't walk away with a bunch of misconceptions about how this all works. | |
Feb 20, 2014 at 1:14 | history | edited | Mike Holt | CC BY-SA 3.0 |
deleted 1 characters in body
|
Feb 20, 2014 at 1:11 | comment | added | ajk |
The no-bracket approach also has the advantage of letting you write clean, clear (imo) one-liners like $the_world_is_flat && echo "you are in flatland!"
|
|
Feb 20, 2014 at 1:09 | history | edited | Mike Holt | CC BY-SA 3.0 |
deleted 1 characters in body
|
Feb 20, 2014 at 1:03 | history | edited | Mike Holt | CC BY-SA 3.0 |
added 2153 characters in body
|
S Feb 14, 2014 at 0:00 | review | Late answers | |||
Feb 14, 2014 at 0:02 | |||||
S Feb 14, 2014 at 0:00 | review | First posts | |||
Feb 14, 2014 at 0:00 | |||||
Feb 13, 2014 at 23:45 | history | answered | Mike Holt | CC BY-SA 3.0 |