Skip to main content
21 events
when toggle format what by license comment
Aug 15, 2017 at 14:32 comment added cas @JillRussek i just ran across this Q again. It turns out that true and false are also bash built-in commands (running type true false says they're built-in, and help true false describes them, and they're mentioned as built-ins in the bash man page). This is also the case in other sh-like shells I tested - dash, ash, ksh, and zsh - huh, i never realised. i'll make use of this in future.
Aug 5, 2017 at 0:36 comment added cas @JillRussek if x; then ... fi runs command x and checks the exit-code. [ is an external command (/usr/bin/[), as is test (/usr/bin/test) ....it just so happens that [ and test are also built-in to many modern shells (including bash) so there's no need to call an external program for them. What this means is that if "$sflag"; ... actually runs the command specified by the value of $sflag (in this case, that will be either true or false). So, yes, less efficient because it needs to run external commands.
Aug 4, 2017 at 15:30 comment added user243760 @cas are you saying that setting variables to true or false causes an external program called /bin/true , /bin/false, to run, which isn't a part of bash? Are basically saying this is less efficient? How does it affect the call stack? Do you know? I'm basically asking how it would be less efficient -- not because I think you're wrong. I'm just curious.
Aug 4, 2017 at 15:11 comment added cas because with the way you're doing it, you're running an external program (/bin/true or /bin/false) and then checking the exit code instead of just checking the value of a bash variable within bash. i.e. fork and check vs just check.
Aug 4, 2017 at 14:57 comment added user243760 @cas I like the simplicity of that, although, I'd like to know your reasoning behind why it's better. Also, I'd still use terdon's if "$sflag" instead of if [ "$sflag" == 1 ] because that seems sloppy. If (true) is more elegant to me than if (true == true), which seems like extraneous, nutritionally devoid calories.
Aug 4, 2017 at 1:43 comment added cas rather than set the flag variables to 'true' or 'false' so that you can run them to get the exit code, it's better to initialise them to 0 and have the getopts loop set them to 1, then test them like if [ "$sflag" == 1 ] ; then ... fi. Alternatively, set them to the empty string '' and have getopts set them to anything. then you can test if they're non-empty (if [ -n "$sflag" ]; then ... fi)
Aug 3, 2017 at 15:31 vote accept CommunityBot
Aug 3, 2017 at 15:14 answer added terdon timeline score: 2
Aug 3, 2017 at 15:03 comment added user243760 @Archemar (when I changed it to brackets it worked)
Aug 3, 2017 at 14:56 comment added jesse_b use brackets...and better commands (the ones I suggested :p)
Aug 3, 2017 at 14:56 comment added user243760 @Archemar if "x$sflag" = "x" ; and if "x$sflag" = x isn't working for me anyway. shrug seemed clever in theory. Just says "x command not found". So @Jesse_b wins
Aug 3, 2017 at 14:50 comment added jesse_b @Archemar Why? You should do if [[ -n $flags ]]; then from now on. @Jill How is it sloppy? It is checking for a particular result and therefore will have less chance of unexpected outcomes. With your script you should be able to do if [[ -n $sflag ]]; then without an issue but I think that is sloppier because it will accept any non-null value as true.
Aug 3, 2017 at 14:49 comment added user243760 @Archemar I like that idea
Aug 3, 2017 at 14:48 comment added user243760 @Jesse_b yeah... that seems so sloppy though. I was hoping the falsy idea was relevant to bash :/
Aug 3, 2017 at 14:47 comment added Archemar I check emptyness using if [ "x$flags" = x ]; then echo empty ; else echo not empty ; fi
Aug 3, 2017 at 14:46 comment added jesse_b You could do if [[ "$sflag" = "true" ]]; then
Aug 3, 2017 at 14:45 comment added user243760 @Jesse_b just trying to check if the flag is true... is the empty string considered null in bash? if Not, I suppose I could do a check for length...
Aug 3, 2017 at 14:43 comment added user243760 Okay. Do you have any suggestions for re-implementation? That would be fantastical.
Aug 3, 2017 at 14:42 comment added jesse_b What are you trying to check for? You can do if [[ -n $sflag ]]; then to check if the variable is not null or -z to check if it is.
Aug 3, 2017 at 14:40 comment added Ziazis you compare to nothing the if, and if if is empty "there is no such command"
Aug 3, 2017 at 14:34 history asked user243760 CC BY-SA 3.0