by hi-v-rocknroll · 2y ago · 0 comments
In today's world where bash is and does supplant POSIX for all intents and purposes, use [[ and (( because they're part of the interpreter and more flexible.
In short, because the following works despite quoting and will execute the echos
a=(); x='a[$(echo >&2 what; echo 0)]'; [[ 'x' -eq 0 ]]
I'm back to use the single [ for the arithmetic binary operators.> arg1 OP arg2
> OP is one of -eq, -ne, -lt, -le, -gt, or -ge. [...] When used with the [[ command, Arg1 and Arg2 are evaluated as arithmetic expressions (see ARITHMETIC EVALUATION above).
> ARITHMETIC EVALUATION
> Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax.
> The value of a variable is evaluated as an arithmetic expression when it is referenced
> Arrays
> The subscript is treated as an arithmetic expression that must evaluate to a number.
> [[ expression ]]
> The shell performs [...] arithmetic expansion, command substitution [...] on those words.
Given that reading, that both subscripts and values of variables in arithmetic expression are treated as arithmetic expressions, and that command substitution is peformed after arithmetic expansion in `[[`, I kinda expected the following to work:
$ x='$(echo >&2 what; echo 0)'; [[ 0 -eq x ]]
bash: [[: $(echo >&2 what; echo 0): syntax error: operand expected (error token is "$(echo >&2 what; echo 0)")
I don't see in the docs what it is about array subscripts that makes them special with regards to command substitutions in arithmetic expressions.>> The shell performs [...] arithmetic expansion, command substitution [...] on those words.
> [...] and that command substitution is peformed after arithmetic expansion in `[[` [...]
You're right. That doesn't apply. I was equating "arithmetic expansion" with "evaluation of arithmetic expressions", but I see now the former is specifically about `$(())`.
You will understand why when you try this with /bin/sh on Debian/Ubuntu.
This incompatibility was introduced for reasons of speed and standards compliance.
There is a time and a place to rely on advanced shell features, but it should not be the default preference.
PS: Be good, or I'll replace you with a very small zsh script. ;) Interestingly, it possible to dynamically add additional native commands to both zsh and bash in other languages. LuaBash and bash-loadables are examples.
They also fail with busybox.
There are many subtle differences between BASH and Korn, so much will not run without rewrite on Android's mksh.
Because you have not needed portable scripts yourself does not imply that the need is not vast.
1) Write a bash script and put /bin/bash in your shebang
2) Write a POSIX shell script and integrate checkbashisms into your editor.
Neither is more correct than the other.
Bashisms are powerful, flexible, and occasionally performant.
They're also non-portable, and you're apt to discover this whilst in an emergency situation with limited resources, most of all time. Having to edit shell scripts into POSIX conformance on a smartphone SSH session is No Bueno.
There are an awful lot of systems which have either no, or old, Bash. Most prevalent of the latter is MacOS / OSX, which is now nagging me on each shell initialisation that the current supported standard is not bash but zsh. Scripts linked to /bin/bash will of course run, but the last update was version 3.2.57(1) if my system reports correctly. The most recent GNU Bash release is 5.3 according to <https://ftp.gnu.org/gnu/bash/>. And that's hardly the only old Bash floating around.
On many systems, including many routers, DSL/cable modems, and other embedded devices, shell is some alternate variant (dash is /bin/sh on Debian, ash on RHEL's initrd IIRC, which last I fought with it was a script-only interpreter, not capable of running interactive commands, fucking annoying as hell). And there are still legacy Unix systems running Bourne, ksh, or other shells. Not to mention the phenomenal number of systems for which busybox provides much if not all of the userland environment, including /bin/sh. And is not bashism-capable.
Ignoring POSIX works fantastically until it doesn't. Caveat scriptor.
Since David Korn retired, it has been adrift with few updates.
The dash shell can be compiled with libedit to implement "set -o vi" (which is also POSIX), and is pleasant to use this way.
People say that bash is already installed on the system. Then people like you point out that it isn't.
Okay so just ... don't do that? You're meant to put /bin/bash in the shebang ... because it's a bash script. I don't understand this self-inflicted problem; obviously if you run a bash script with sh, it won't do what you want! They're different things! You may as well complain it won't run under /bin/python.
If you use arrays, coprocesses, or advanced conditional [[ then your script will fail.
The rules for #!/bin/sh as POSIX.2 are here:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
Will this fail on rhel? No, as bash implements #!/bin/sh. Will it fail on Ubuntu? Absolutely.
And I'm saying just don't do that! If you use #!/bin/sh, it won't work, so don't use #!/bin/sh! You're writing Bash, so use #!/bin/bash! Because it's Bash. Not sh.
Bash and POSIX sh are different languages. If you write a script in language A, but try to run it as language B, it obviously won't work! And it's your own stupid fault for trying. Why is this such a difficult concept?
Literally I do not understand, why people write something in Bash then put the wrong shebang line at the top, on purpose. And then it doesn't work right, and then they act like this is a problem with Bash. Instead of their own fault, for trying to execute it with the wrong interpreter. Like .. just stop punching yourself in the face on purpose! Stop being stupid!
The syntax inside [[ is different though.
The [[ test syntax is not implemented in dash. It is listed as a future reserved word in the POSIX shell standard, but is not (yet?) specified.
(Google: POSIX shell)
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
If you pull the HTML file off the above link, you will get a directory listing of all of the POSIX.2 utilities (tar, sed, awk, etc.). The options that you find in the specifications are maximally portable.