Writing shell scripts for the POSIX shell, sh
(also commonly referred to as the Bourne shell), without a #!
is easy as can be. As I discussed previously, any file not identified as an executable is required to be passed to sh
already, so a conforming POSIX system will just work.
This also conveniently side-steps a problem that can arise by hardcoding a path, such as /bin/sh
, into scripts. Some people are under the misconception that POSIX requires /bin/sh
to exist. It does not. What POSIX does require is that a utility named sh
can be started via one of the exec()
functions in the default PATH
(which can be retrieved by using _CS_PATH
as an argument to confstr()
). POSIX doesn't require it to be in /bin
, or even for /bin
to exist. So, bottom line, leave the #!
out of your POSIX sh
scripts completely. It's unnecessary. Observe:
$ cat hello.sh
printf 'hello, sh\n'
$ sh ./hello.sh
hello, sh
$ chmod +x hello.sh
$ ./hello.sh
hello, sh
Copyright © 2019 Jakob Kaivo <jakob@kaivo.net>