Here's a #bash pop quiz.
-
-
Jonathan Lamothereplied to Scott Williams 🐧 last edited by
@vwbusguy My server doesn't support polls, but here's my thinking:
My initial instinct is to go with syntax error, but I doubt you'd post this if it were that uninteresting, so I'm goint to go with "false" because 0 is truthy?
-
Gonzalo Nemmi :runbsd:replied to Scott Williams 🐧 last edited by
@vwbusguy as I understand it, what you are testing there is: what does the test return to the OS?
For that matter [[ 1 ]] is just as good as [[ 0 ]] .. they will both return "0" to the OS and print "true".
Now .. if you go and test [[ 2 != 2 ]] ... you'll get a "1" in return ( check with 'echo $?' ) and *then* you'll get a "false".
All this to say: I went with option 2
I may be utterly wrong though ..
-
-
@vwbusguy Need to check what A && B || C does. Is it A && (B || C) or (A && B) || C? In short: I don't know
-
@dneary Not the first one but I guess the second one technically. There's no trickery in the ternary with this one.
-
@vwbusguy This one made me think, play around in the terminal, and try to read bash documentation.
Both [[ 1 ]] and [[ 0 ]] evaluate to true, while [[ '' ]] does not.
Feel free to post more of these
-
@jbowen If this inspired someone to play around and read the docs, then I'm chalking that up as a success!
-
@vwbusguy The guy who runs the indently.io Python YouTube channel posts little quizzes like this one quite often.
They're like little nucleation points for getting me started down some rabbit hole (as was your poll).
-
Space Catitude 🚀replied to Scott Williams 🐧 last edited by
I really don't do bash scripting much, but typing this into the shell actually gives me:
bash: [[1]]: command not found
-
Scott Williams 🐧replied to Space Catitude 🚀 last edited by
@TerryHancock You need a space after [[ and before ]] . It's a bash-ism shorthand for the test command.
-
@jbowen
Back when I was on Twitter, I used to do bash quizzes regularly after I was inspired by someone that did it with python. -
Gonzalo Nemmi :runbsd:replied to Gonzalo Nemmi :runbsd: last edited by
@vwbusguy ok, so .. now with time on my hands I understand that I can confirm that my previous assumption was correct, that is to say, that the ' && echo "true" ' will only execute if the previous command ( a test in this case ) didn't fail and returned with an exit status of "0".
The same applies to while and if. The value tested by the while and if commands is the "exit status" of the last simple command following the "while" or "if".
-
Scott Williams 🐧replied to Scott Williams 🐧 last edited by
In C based languages, 1 is truthy, but in bash 0 is truthy. However, in this case, the same result happens whether or not it's a 0 or a 1 because without any expression, bash will treat it as a string test which returns true for any non-zero length string.
#4 is the correct answer.
Shout out to @fedops for getting it right first in the comments!
-
Scott Williams 🐧replied to Jonathan Lamothe last edited by
Scott Williams 🐧 (@[email protected])
Content warning: The answer
Mastodon (mastodon.online)
-
Fabio Valentinireplied to Scott Williams 🐧 last edited by
@vwbusguy thanks - one more reason for why I think bash is cursed
-
Scott Williams 🐧replied to Fabio Valentini last edited by
@decathorpe They're all cursed.
-
R.L. Dane :Debian: :OpenBSD: 🍵replied to Fabio Valentini last edited by
Disagree. The whole point of the
[[ ]]
syntax is to examine and compare strings (and also filenames). There's no expectation of numerical or boolean analysis.Now, if you said that bash is cursed because of the string substitution syntax, I'm with you on that one. Its horrifying.
-
Scott Williams 🐧replied to R.L. Dane :Debian: :OpenBSD: 🍵 last edited by [email protected]
@rl_dane @decathorpe I disagree. If you `man test` you'll see that numerical analysis is indeed supported and I use it regularly as a way to evaluate timestamps (converting to epoch seconds and using gt, lt, etc).
Here's an example of where I use it for both boolean and numerical analysis:
Calculates a new serial for a bind zone SOA. Assumes single zone per file.
Calculates a new serial for a bind zone SOA. Assumes single zone per file. - calculate_new_bind_serial.sh
Gist (gist.github.com)
-
R.L. Dane :Debian: :OpenBSD: 🍵replied to Scott Williams 🐧 last edited by
But
test
is analogous to[ ]
, not[[ ]]
.