4 ms·
> that's why we start bash scripts with #!/bin/bash That will also fail spectacularly, as bash does not behave the same when called as /bin/bash as it does whe
by chasil 5y ago
> that's why we start bash scripts with #!/bin/bash
That will also fail spectacularly, as bash does not behave the same when called as /bin/bash as it does when it is /bin/sh.
I have principally noticed that aliases are not expanded in scripts unless a shopt is issued, which violates POSIX.
Forcing POSIXLY_CORRECT might also help.
- ericbarrett 5y agoNot sure what you are saying, bash behaves as bash when invoked as /bin/bash, and Bourne-shell-ish when invoked as /bin/sh. Lots more detail in the man page. I've never seen use of aliases in a bash script...? They are generally for CLI convenience.
- chasil 5y agoAlias expansion within scripts is mandated by POSIX. When bash is not in POSIX mode, it violates the standard. $ ll /bin/sh lrwxrwxrwx. 1 root root 4 Nov 24 08:40 /bin/sh -> bash $ cat s1 #!/bin/sh alias p=printf p hello\\n $ cat s2 #!/bin/bash alias p=printf p world\\n $ ./s1 hello $ ./s2 ./s2: line 3: p: command not found
- ericbarrett 5y agoThat's very nice, it's POSIX-compliant when invoked as #!/bin/sh and sane when invoked as #!/bin/bash — exactly what I'd want.
- chasil 5y agoIf you want a portable script, then you don't want that behavior.
- ericbarrett 5y agoThen I wouldn't put #!/bin/bash at the top.
- chasil 5y agoYour problem there is Ubuntu/Debian. A bash script that runs as /bin/sh might not run as /bin/bash, until you set POSIXLY_CORRECT. Easiest solution there is to move it to a better shell, usually a Korn variant.
- jjnoakes 5y agoSure but someone putting #!/bin/bash at the top of their script, written for non-POSIX bash, won't have that issue...
- gh02t 5y agoI would assume if you put /bin/bash as your shebang that you're expecting to get bash-isms. I think the problem you're complaining about (which is a real one) is people putting /bin/sh and expecting bashisms. Debuntu being problematic here is more a side effect of bad practice.
- chasil 5y agoBash has a POSIX mode. Knowing when to switch into and out of this mode, and what impact it has, is a more advanced subject that should not burden those learning the Borne family. It is better to start with Almquist, or another pure POSIX implementation, with documentation specific to standard adherence. More advanced shell features should wait.
- gh02t 5y agoI have mixed feelings. On paper I agree with you, people should start with POSIX shell. In practice I'm not sure how relevant that is anymore. I'm not really convinced bash should be the default thing people learn, but I think there's a decent argument that people should just start off with the shell they're going to actually use. You should, however, be aware that there is a distinction, and if you're learning/writing bash and not POSIX shell you should specify /bin/bash not /bin/sh. But you don't necessarily need to know all the nuances of how bash differs unless you have a need to write POSIX compliant shell.
- chasil 5y agoFor me, I just want a shell that works. Without a nuanced understanding of standards, extensions, and platform availability, new bash users will get large amounts of shell usage that doesn't work. To avoid that frustration, learn POSIX. That works everywhere that matters.