Here's a #bash pop quiz.
-
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[[ ]]
. -
Scott Williams 🐧replied to R.L. Dane :Debian: :OpenBSD: 🍵 last edited by [email protected]
@rl_dane @decathorpe [[ supports everything [ does. [[ is the bash-ism where [ is POSIX, and test in bash is a built-in, IIRC. None of this negates what I said in my previous post.
-
R.L. Dane :Debian: :OpenBSD: 🍵replied to Scott Williams 🐧 last edited by
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 🐧replied to R.L. Dane :Debian: :OpenBSD: 🍵 last edited by
@rl_dane @decathorpe No, what [[ adds is stuff like && and || for and/or logic and using ! for negation. It supports the same evals as test, and I should also note that "test", [, [[ are built-ins for bash, so it's not actually calling the test binary in a script unless you give the full path to it.
-
DHeadshot's Altreplied to R.L. Dane :Debian: :OpenBSD: 🍵 last edited by
@rl_dane
I just use `test` directly for everything as it makes more sense, so the above would be `test ! -z 1 && echo "true" || echo "false"`, which is actually readable!
@vwbusguy @decathorpe -
Scott Williams 🐧replied to DHeadshot's Alt last edited by [email protected]
@ddlyh @rl_dane @decathorpe test -n would be the equivalent one in this case.