4 ms·
>You will understand why when you try this with /bin/sh on Debian/Ubuntu. Okay so just ... don't do that? You're meant to put /bin/bash in the shebang ... beca
by _dain_ 2y ago
>You will understand why when you try this with /bin/sh on Debian/Ubuntu.
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.
- chasil 2y agoIf you use #!/bin/sh then you must understand the POSIX.2 shell standard. 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/V3_chap02.html 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.
- _dain_ 2y ago>If you use #!/bin/sh ... 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!
- chasil 2y agoBash developed before POSIX.2, so the old behavior of bash that is incompatible is suppressed when it runs as #!/bin/sh or when POSIX mode is otherwise set. My particular bugbear is "alias p=printf" which works in scripts when bash is /bin/sh but fails when it is /bin/bash. This is because bash historically did not honor alias in scripts. The solution is to set POSIX mode. So, you see, there are two bash shells, and you likely do not know know the pre-POSIX shell as well as you might think. Just writing for POSIX.2 is more straightforward.