I have read that if you want to use Bash in a portable way you should use the shebang:
#!/usr/bin/env bash
But now I am wondering: When I want to state explicitly that I do not rely on Bash, but instead, wrote a POSIX compliant script, should I use:
#!/bin/sh
Or is #!/usr/bin/env sh
preferable here too?
You should use #!/usr/bin/env bash for portability: different *nixes put bash in different places, and using /usr/bin/env is a workaround to run the first bash found on the PATH .
#!/bin/bash is a sequence of characters (#!) called shebang and is used to tell the Linux OS which interpreter to use to parse the rest of the file. You will always see #!/bin/bash or #!/usr/bin/env bash as the first line when writing or reading bash scripts. Shebang starts with #!
POSIX defines the application programming interface (API), along with Unix command line shells and utility interfaces. This ensure software compatibility with flavors of Unix and other operating systems. The POSIX shell is implemented for many UNIX like operating systems.
POSIX Shell is a command line shell for computer operating system which was introduced by IEEE Computer Society. POSIX stands for Portable Operating System Interface. POSIX Shell is based on the standard defined in Portable Operating System Interface (POSIX) – IEEE P1003.
The informative section of the POSIX specification for
sh: Application Usage states
that you cannot rely on the sh
executable being installed at /bin/sh
.
Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh, and should be determined by interrogation of the PATH returned by getconf PATH, ensuring that the returned pathname is an absolute pathname and not a shell built-in.
For example, to determine the location of the standard sh utility:
command -v sh
However, instead of suggesting the use of env
to use the appropriate PATH,
it suggests that shell scripts should be modified at installation time to use
the full path to sh
:
Furthermore, on systems that support executable scripts (the "
#!
" construct), it is recommended that applications using executable scripts install them using getconf PATH to determine the shell pathname and update the "#!
" script appropriately as it is being installed (for example, with sed).
I mostly write POSIX shell scripts and, in practice, every GNU/Linux system
(Red Hat and Debian-based) – and others such as Cygwin and OS X – has a
POSIX-compliant sh
either installed to /bin/sh
or available as a soft or
hard link at this path. I’ve never needed to use env
to cater for systems
where sh
does not use this path.
There may be some Unix systems where a POSIX-compliant sh
is not available
as /bin/sh
. The POSIX specification suggests that it might be installed on
some systems as /usr/xpg4/bin/sh
. As I understand it, this is (was?) true
for Solaris systems where /bin/sh
is an earlier version of the Bourne shell
which predates POSIX. In this case, using env sh
would not be guaranteed to help as it could still find the Bourne shell (at /bin/sh
) before the POSIX shell at /usr/xpg4/bin/sh
.
If you’re writing POSIX shell scripts for common Unix and Linux operating
systems, simply use #!/bin/sh
as the shebang.
In rare cases where /bin/sh
is a Bourne shell instead of a POSIX-compliant
shell, you would have to modify the shebang to use the appropriate full path
to the POSIX shell.
In either case, there’s no benefit to using #!/usr/bin/env sh
– and would be
more likely to fail than simply using #!/bin/sh
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With