Skip to main content
37 events
when toggle format what by license comment
May 21 at 17:48 history edited Benjamin Loison CC BY-SA 4.0
Add a link to the mentioned comment
Dec 4, 2023 at 3:37 comment added xuiqzy Please add an explanation to your answer why this is your recommended solution compared to other options and how it behaves and works in bash. As far as I understand it's just a string comparison, so quoting the strings would be way clearer, when the word true implies there is some special syntax going on here that executes the true command or that true might be a keyword in bash.
Jun 12, 2022 at 14:05 comment added Tom Shaw There's a serious exception - the_world_is_flat = true surely would cause the exception stupidity=true to also be false.
Nov 7, 2021 at 20:05 comment added alper would it be same if I apply it for false?
May 27, 2021 at 12:28 comment added Gauthier Not assigning the_world_is_flat first still works as expected. Why? I'd expect the condition evaluation to result in a syntax error.
Mar 26, 2021 at 10:03 comment added zaTricky Doesn't the single = inside the [] need to be doubled == ?
Oct 21, 2020 at 19:17 comment added Jemar Jones Super casual flat earther comment, nothing to see here
Aug 17, 2020 at 12:46 review Suggested edits
Aug 18, 2020 at 10:00
Apr 6, 2020 at 15:31 comment added tahiche Note the importance of the space... "if [ "$the_world_is_flat"=true ]" (no space around = operator) will always be true. This gets me every time
May 27, 2019 at 23:27 history edited Gurpartap Singh CC BY-SA 4.0
correct typo: if -> fi
May 17, 2019 at 14:13 history edited granadaCoder CC BY-SA 4.0
edited body
Jan 4, 2018 at 22:52 comment added Timothy Swan [[ -e file.txt ]] ; echo "$?" Prints 0 if file is there. This is necessary to include as part of the answer because otherwise people won't know how to store the boolean.
Nov 30, 2017 at 1:15 comment added Levi @HubertGrzeskowiak "It is executing a command every time you check the value": this is only true for very old shells. true (and false) are built-in commands in bash (and virtually every other shell): no new process is started.
Nov 3, 2017 at 9:12 comment added Hugo G About the newer answer: it is simple string comparison. When in bash, you should rather use double square brackets and quote everything so people know it's strings and nothing special.
Nov 3, 2017 at 9:11 comment added Hugo G DO NOT USE THE ORIGINAL ANSWER! It is executing a command every time you check the value and it's easy to forget how it works and put square brackets around it (which breaks it all). Short answer is: bash does not have booleans. It only has builtin testing ([ ], [[ ]], test) which evaluates to a boolean'ish exit code. You cannot define that boolean, though.
Sep 13, 2017 at 12:29 comment added andras.tim I think this answer can kill the kitties because the true in the_world_is_flat assignment is a command in the original answer! Let's check with the_world_is_flat=date...
May 23, 2017 at 10:31 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Dec 13, 2016 at 9:17 comment added Loenix VAR=true; $VAR && echo "TRUE" || echo "FALSE"; Show True and VAR=false; $VAR && echo "TRUE" || echo "FALSE"; Show False. So this is working properly...
Mar 4, 2016 at 13:41 comment added dolmen The original answer was just better and completely safe as long as the assignments of the variable are completely controlled inside your script.
Jun 8, 2015 at 23:02 comment added wisbucky @ajk The reason that a lot of the comments seem incorrect is because miku's original answer was revised by another user on Feb 12, 2014, so it invalidated most of the previous comments.
S Jun 8, 2015 at 22:52 history suggested wisbucky CC BY-SA 3.0
The Revised Answer on Feb 12, 2014 caused a lot of confusion because there were many comments and other answers that referenced the Original Answer. Including the original answer helps the reader understand the context of the older comments and answers.
Jun 8, 2015 at 21:32 review Suggested edits
S Jun 8, 2015 at 22:52
Apr 1, 2015 at 9:58 comment added Quolonel Questions This code is not the same and does not work in the same way as the linked article. The linked code calls a program by the name stored in a variable but the code in this answer is just string comparison.
Jul 9, 2014 at 16:51 history edited miku CC BY-SA 3.0
added 16 characters in body
Feb 21, 2014 at 8:58 history edited miku CC BY-SA 3.0
deleted 76 characters in body
Feb 20, 2014 at 1:09 comment added ajk Lots of incorrect information, here. /bin/true isn't being used effectively. See Dennis' answer.
S Feb 12, 2014 at 14:13 history suggested lensovet CC BY-SA 3.0
updating per other answer explaining why the original code here had issues
Feb 12, 2014 at 14:12 review Suggested edits
S Feb 12, 2014 at 14:13
Jan 21, 2014 at 13:44 history edited miku CC BY-SA 3.0
added 54 characters in body
Jan 18, 2014 at 23:04 comment added Dennis -1, see my answer for an explanation.
Dec 31, 2013 at 9:10 history edited miku CC BY-SA 3.0
added 33 characters in body
Jul 31, 2013 at 12:16 history edited miku CC BY-SA 3.0
added 74 characters in body
Jun 30, 2012 at 9:07 comment added michael @pms The operators "-o" and "-a" are only for the "test" command (aka "[]"). Instead, this is "if + command", without the "test". (Like "if grep foo file; then ...".) So, use the normal && and || operators: # t1=true; t2=true; f1=false; # if $t1 || $f1; then echo is_true ; else echo is_false; fi; (returns "true", since t1=true) # if $t1 && $f1 || $t2; then echo is_true ; else echo is_false; fi (returns "true", since t2=true) . Again, this ONLY works because "true"/"false" are bash-builtins (returning true/false). You can't use "if $var..." unless var is a cmd (ie, true or false)
Sep 7, 2010 at 1:33 history edited miku CC BY-SA 2.5
deleted 1 characters in body
Jul 19, 2010 at 5:30 vote accept hassaanm
Jun 2, 2010 at 4:16 comment added Dennis Williamson To explain what is happening: the if statement is executing the contents of the variable which is the Bash builtin true. Any command could be set as the value of the variable and its exit value would be evaluated.
Jun 1, 2010 at 21:58 history answered miku CC BY-SA 2.5