Skip to main content
19 events
when toggle format what by license comment
Oct 23, 2023 at 23:22 comment added profPlum This is by far the best answer, it overcomes weakness of bool=true & bool=false b/c if bool is unset (i.e. bool= then $bool && echo True evaluates true! Also if you just use bool=True; [[ $bool ]] && echo True & leave bool= (empty) if false, sometimes you might accidentally set the variable to something like bool=False which will evaluate true... And of course all other methods have tons of syntactical baggage.
Mar 29, 2021 at 19:59 comment added Bill Gale This answer is the closest to the OP where they were interested in if ! variable and the arithmetic expressions support that. All the string approaches suffer from having to do string companions to "false" or using else constructs, if you interest is in false only, an else construct would be an awkward way to achieve that.
Sep 2, 2019 at 6:45 comment added Quolonel Questions @wjandrea That's the opposite of a problem because now you have a mechanism to identify bugs in your code.
Sep 2, 2019 at 2:20 comment added wjandrea There is one problem with this: invalid values cause syntax errors, e.g. s=/home; ((s)) outputs bash: ((: /home: syntax error: operand expected (error token is "/home")
Aug 9, 2019 at 11:30 comment added mgutt sh --help returns GNU bash, version 4.3.48(1)-release-(x86_64-pc-linux-gnu) so its simply bash (this is the shell of a Synology NAS). And the question was related a "shell" and not specific shell variant. So in which shell does your code work?
Aug 8, 2019 at 11:09 comment added mgutt Does not work for me. It returns true and not true?! ash-4.3# false=0 ash-4.3# true=1 ash-4.3# ((false)) && echo false ash-4.3# ((true)) && echo true true ash-4.3# ((!false)) && echo not false ((false=0)) && echo not false ash-4.3# ((!true)) && echo not true ((true=1)) && echo not true not true
Jun 30, 2019 at 17:38 comment added WinEunuuchs2Unix ((Debug)) && echo stuff is so much simpler than the longer version[[ "$fDebug" == true ]] && echo stuff. You've got my vote.
Jun 7, 2019 at 8:01 comment added alfiogang I write this script for print greating on the first time (usefull in huge directory for command to signal progress). limit=10 first=1 for ((i=1;i<=$limit;i++)); do ((first)) && echo "Hello" && first=0 echo $i done
Nov 3, 2017 at 13:59 comment added Hugo G In case you are referring to the exception of certain chained commands from the special error-intolerant mode, I think relying on the next editor to know these implications is too risky and not worth the seemingly better syntax. I've seen arithmetic expressions on their own line a few times too much, I guess
Nov 3, 2017 at 13:56 comment added Hugo G I think I am well aware of the implications of '-e' and I would still recommend it for multiple reasons
Nov 3, 2017 at 13:55 comment added Hugo G @quolonel thank you for the very helpful resource. Of course my understanding is limited - it is human nature to be limited in all understanding no matter the domain. However, would you mind telling me which of my statements lead you to the assumption that my understanding of this particular matter is incomplete?
Nov 3, 2017 at 13:28 comment added Quolonel Questions @HubertGrzeskowiak Since you have demonstrated very limited understanding I suggest you read why set -e is a bad idea.
Nov 3, 2017 at 9:22 comment added Hugo G Also you can't use test flags (see man test) with this. It is for arithmetical expressions only
Nov 3, 2017 at 9:19 comment added Hugo G Big downside of this: The double braces have exit code 1 if the result of the expression is 0, so if you are using strict error checking (set -e), which you totally should, this will lead to unforeseen errors.
Sep 5, 2015 at 6:02 comment added Peter Cordes (()) expands variables recursively, which I wasn't expecting. foo=bar; bar=baz; ((foo)) && echo echo prints nothing, but it's true with baz=1. So you can support foo=true and foo=false as well as 0 or 1 by doing true=1.
Sep 5, 2015 at 5:42 comment added Peter Cordes For interactive use, like one-liners, make sure to leave a space after !, or it will do history expansion. ((! foo)) works, so does ! ((foo)). I love this solution, BTW. Finally a concise way to do boolean variables. ((foo || bar)) works as expected.
May 26, 2015 at 13:03 comment added Quolonel Questions @TrevorBoydSmith Why didn't you just say, "pros: everything, cons: nothing". Would save depreciation costs on your keyboard and monitor in the long run.
May 25, 2015 at 12:23 comment added Trevor Boyd Smith pros: (1.) behaviour is similar to C's way of handling bools, (2.) syntax is very concise/minimal (does not require a right-hand variable and operators like '=' or '=='), (3.) <subjective>for me I understand what happens without a long winded explanation ... contrast to Miku and Dennis' answers which both seem to require long winded explanations</subjective>
Nov 13, 2014 at 23:44 history answered Quolonel Questions CC BY-SA 3.0