Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended POSIX sh shebang

Tags:

shell

posix

sh

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?

like image 272
JepZ Avatar asked Nov 02 '18 09:11

JepZ


People also ask

What is the preferred shebang?

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 .

What is the correct shebang line for a bash script?

#!/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 #!

What is a Posix compatible shell?

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.

What is Posix shell script?

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.


1 Answers

Formal perspective

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).

In practice

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.

Summary

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.

like image 138
Anthony Geoghegan Avatar answered Nov 05 '22 17:11

Anthony Geoghegan