Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active October 16, 2019 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/66a3723e5a69fe355f2cade31a1070e8 to your computer and use it in GitHub Desktop.
Save Integralist/66a3723e5a69fe355f2cade31a1070e8 to your computer and use it in GitHub Desktop.
[Bash Hash Bang] #bash #hash #bang

A typical bash script will identify where its interpreter can be found...

#!/bin/bash

But this doesn't always work because bash might be installed in a different location.

For example, macOS has an old version of bash (or in more modern releases it has swapped bash completely for zsh) and so a user might manually install an updated version of bash which will go into a different directory...

#!/usr/local/bin/bash

To make your scripts portable across different systems you should reference the env executable and pass it the command you want it to locate (i.e. bash in this case)...

#!/usr/bin/env bash

The env executable will use whatever bash executable appears first in the running user's $PATH variable.

If you have two version of bash installed (bash1 in /bin and bash2 in /usr/local/bin) and your PATH was set like so: /home/foo/bin:/usr/local/bin:/usr/bin:/bin than bash2 would execute the script.

You can use either type env or command -V env to locate where the env command is installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment