Here's a #bash pop quiz.
[[ 1 ]] && echo "true" || echo "false"
- Error for invalid expression (14%, 10 votes)
- true because 1 is truthy (31%, 22 votes)
- false because 0 is truthy (15%, 11 votes)
- true because 1 is a non-zero length string (37%, 26 votes)
Scott Williams 🐧 reshared this.
Jonathan Lamothe
in reply to Scott Williams 🐧 • •@Scott Williams 🐧 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?
Scott Williams 🐧
in reply to Jonathan Lamothe • • •@me
mastodon.online/@vwbusguy/1137…
Scott Williams 🐧
2025-01-04 07:10:23
fedops 💙💛
in reply to Scott Williams 🐧 • • •Gonzalo Nemmi :runbsd:
in reply to Scott Williams 🐧 • • •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 .. 😱
Gonzalo Nemmi :runbsd:
in reply to Gonzalo Nemmi :runbsd: • • •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 🐧
in reply to Gonzalo Nemmi :runbsd: • • •@gnemmi mastodon.online/@vwbusguy/1137…
Scott Williams 🐧
2025-01-04 07:10:23
Dave Neary
in reply to Scott Williams 🐧 • • •Scott Williams 🐧
in reply to Dave Neary • • •Jason Bowen
in reply to Scott Williams 🐧 • • •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 😀
Scott Williams 🐧
in reply to Jason Bowen • • •Jason Bowen
in reply to Scott Williams 🐧 • • •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).
Scott Williams 🐧
in reply to Jason Bowen • • •Back when I was on Twitter, I used to do bash quizzes regularly after I was inspired by someone that did it with python.
Space Catitude 🚀
in reply to Scott Williams 🐧 • • •I really don't do bash scripting much, but typing this into the shell actually gives me:
bash: [[1]]: command not found
Scott Williams 🐧
in reply to Space Catitude 🚀 • • •Scott Williams 🐧
in reply to Scott Williams 🐧 • • •Sensitive content
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!
Fabio Valentini
in reply to Scott Williams 🐧 • • •Sensitive content
R.L. Dane :Debian: :OpenBSD: 🍵
in reply to Fabio Valentini • • •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 🐧
in reply to R.L. Dane :Debian: :OpenBSD: 🍵 • • •Sensitive content
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:
gist.github.com/vwbusguy/bbfa0…
Calculates a new serial for a bind zone SOA. Assumes single zone per file.
GistR.L. Dane :Debian: :OpenBSD: 🍵
in reply to Scott Williams 🐧 • • •test
is analogous to[ ]
, not[[ ]]
.Scott Williams 🐧
in reply to R.L. Dane :Debian: :OpenBSD: 🍵 • • •Sensitive content
R.L. Dane :Debian: :OpenBSD: 🍵
in reply to Scott Williams 🐧 • • •Sensitive content
My understanding is that
test
|[ ]
was branched off into[[ ]]
for string/filename analysis/comparison, and(( ))
for numerical operations.[[ ]]
does seem to retain the legacy-gt
|-lt
operators oftest
|[ ]
, which is kinda cursed, admittedly.Scott Williams 🐧
in reply to R.L. Dane :Debian: :OpenBSD: 🍵 • • •Sensitive content
DHeadshot's Alt
in reply to R.L. Dane :Debian: :OpenBSD: 🍵 • • •Sensitive content
@vwbusguy @decathorpe
Scott Williams 🐧
in reply to DHeadshot's Alt • • •Sensitive content