I recently noticed that many scripts are using /usr/bin/env in their shebang. I have seen that mainly using Bash and Python, but thus far never in conjunction with POSIX sh (ash, dash,...).

I wonder why, and if my, meant-to-be highly portable, POSIX shell scripts might benefit from the env approach?

Is there a general concensus on whether to use:

  • standard:

  • environment:

    #!/usr/bin/env sh

Let me stress this enough:

I never have seen this with sh.

  • 3
    On macOS, for example, /bin/bash is an antiquated POC, but one can get newer bash via homebrew. So I use #!/usr/bin/env bash if write a script for both my MBP and my Linux PC. But if I'm going to stick to POSIX, then /bin/sh is fine for both; I don't see much of a use case except in some outdated system where /bin/sh might not be a POSIX shell, in which case I doubt /usr/bin/env will work either.
    – muru
    Feb 27, 2020 at 3:51
  • 8
    unix.stackexchange.com/a/77586/70524: "There are two programs whose location you can rely on on almost every unix variant: /bin/sh and /usr/bin/env. Some obscure and mostly retired Unix variants had /bin/env without having /usr/bin/env, but you're unlikely to encounter them. Modern systems have /usr/bin/env precisely because of its widespread use in shebangs. /usr/bin/env is something you can count on."
    – muru
    Feb 27, 2020 at 3:52
  • 2
    @mosvy Which most used unix variant ever doesn't have /bin/sh?
    – Oskar Skog
    Feb 27, 2020 at 6:34
  • 3
    @muru be vary careful: a bash compiled with default parameters is not POSIX compliant, this is why Apple offers a special bash version in /bin/sh that e.g. treats echo correctly. BTW: /bin/sh is not part of POSIX. POSIX rather requires you to do: export PATH=$(getconf PATH) followed by sh to get a POSIX shell.
    – schily
    Feb 27, 2020 at 15:46
  • 2
    @vonbrand 1. POSIX does NOT mandate the existence of /bin/sh. 2. Except for some obscure Chinese SUSE (or Centos?) fork(s) (iirc), no Linux system has ever been "certified" as Unix.
    – mosvy
    Feb 28, 2020 at 16:31

1 Answer 1


Setting to #!/bin/sh will go directly to that file /bin/sh.

Setting to #!/usr/bin/env sh will execute /usr/bin/env with an argument of sh. This will cause the script to be executed by sh in your PATH variable rather than explicitly with /bin/sh.

  • 1
    None of these commands will give you a POSIX shell in case your PATH is not the result from a getconf PATH call.
    – schily
    Feb 27, 2020 at 15:48
  • Nice Answer. It's a common doubt
    – Daniel
    Mar 6, 2020 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.