What is the preferred Bash shebang?
Is there any Bash
shebang objectively better than the others for most uses?
#!/usr/bin/env bash
#!/bin/bash
#!/bin/sh
#!/bin/sh -
I vaguely recall a long time ago hearing that adding a dash to the end prevents someone passing a command to your script, but can't find any details on that.
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
. And sh
is not bash
.
/bin/sh
is usually a link to the system's default shell, which many or most places will be /usr/bin/bash
. However, the original Bourne shell is sh
, so if your script uses some bash
(2nd generation, "Bourne Again sh"), then you should be more specific and use the later. This way, on systems where bash is not installed, your script won't run.
I understand there may be an exciting trilogy of films about this evolution...but that could be hearsay.
Using a shebang line to invoke the appropriate interpreter is not just for BASH. You can use the shebang for any interpreted language on your system such as Perl, Python, PHP (CLI) and many others. By the way, the shebang
#!/bin/sh -
(it can also be two dashes, ie --
) ends bash options everything after will be treated as filenames and arguments.
Using the env
command makes your script portable and allows you to setup custom environments for your script hence portable scripts should use
#!/usr/bin/env bash
Or for whatever the language such as for Perl
#!/usr/bin/env perl
Be sure to look at the man
pages for bash
:
man bash
and env
:
man env
Note: On Debian and Debian-based systems, like Ubuntu, sh
is linked to dash
not bash
. As all system scripts use sh
. This allows bash to grow and the system to stay stable, according to Debian.
Also, to keep invocation *nix like I never use file extensions on shebang invoked scripts, as you cannot omit the extension on invocation on executables as you can on Windows. The file command can identify it as a script.
链接地址: http://www.djcxy.com/p/17470.html下一篇: 什么是首选Bash shebang?