Skip to main content


Here's a #bash pop quiz.

[[ 1 ]] && echo "true" || echo "false"

#Linux #shell #scripting

  • 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)
69 voters. Poll end: 3 days ago

Scott Williams 🐧 reshared this.

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?

in reply to Jonathan Lamothe

@me

mastodon.online/@vwbusguy/1137…


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!


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 .. 😱

This entry was edited (4 days ago)
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".

This entry was edited (4 days ago)
in reply to Gonzalo Nemmi :runbsd:

@gnemmi mastodon.online/@vwbusguy/1137…


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!


in reply to Scott Williams 🐧

Need to check what A && B || C does. Is it A && (B || C) or (A && B) || C? In short: I don't know 😁
in reply to Dave Neary

@dneary Not the first one but I guess the second one technically. There's no trickery in the ternary with this one.
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 😀

in reply to Jason Bowen

@jbowen If this inspired someone to play around and read the docs, then I'm chalking that up as a success!
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).

in reply to Jason Bowen

@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.
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

in reply to Scott Williams 🐧

The answer

Sensitive content

in reply to Scott Williams 🐧

The answer

Sensitive content

in reply to Fabio Valentini

The answer

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.

in reply to R.L. Dane :Debian: :OpenBSD: 🍵

The answer

Sensitive content

This entry was edited (4 days ago)
in reply to R.L. Dane :Debian: :OpenBSD: 🍵

The answer

Sensitive content

This entry was edited (4 days ago)
in reply to Scott Williams 🐧

The answer

Sensitive content

in reply to R.L. Dane :Debian: :OpenBSD: 🍵

The answer

Sensitive content

in reply to R.L. Dane :Debian: :OpenBSD: 🍵

The answer

Sensitive content

in reply to DHeadshot's Alt

The answer

Sensitive content

This entry was edited (3 days ago)

This website uses cookies. If you continue browsing this website, you agree to the usage of cookies.