recentpopularlog in

kme : shebang   6

The #! magic, details about the shebang/hash-bang mechanism |
what's special about #!

#! was a great hack to make scripts look and feel like real executable binaries.

But, as a little summary, what's special about #!? (list mostly courtesy of David Korn)

- the interpretername must not contain blanks
- the length of the #! is much smaller than the maximum path length
- $PATH is not searched for the interpreter
- (apart from an absolute path, the #! line also accepts a relative path,
- and #!interpreter is equivalent to #!./interpreter,
- however, it's not of any practical use)
- the interpreter usually must no be a #! script again
- the handling of arguments in the #! line itself is varying
- the setuid mechanism may or may not be available for the script
- there's no way to express #!$SHELL

And why shebang? In music, '#' means sharp. So just shorten #! to sharp-bang. Or it might be derived from "shell bang". All this probably under the influence of the american slang idiom "the whole shebang" (everything, the works, everything involved in what is under consideration). See also the wiktionary, jargon dictionary or Merriam-Websters. Sometimes it's also called hash-bang, pound-bang, sha-bang/shabang, hash-exclam, or hash-pling (british, isn't it?).

According to Dennis M. Ritchie (email answer to Alex North-Keys) it seems it had no name originally.
And Doug McIllroy mentioned (TUHS mailing list), that the slang for # at Bell Labs most probably was "sharp" at the time.
bourne  bash  posix  shell  shebang  unix  linux  shellscripting  history  butwhy  explained 
february 2019 by kme
scripting - The way to use `/usr/bin/env sed -f ` in shebang? - Unix & Linux Stack Exchange |
You can't use 'env', so just '#!/usr/bin/sed -f' should be OK.

The problem with that is (as mentioned in the top-rated answer), you can't include more than one argument in a shebang, so you need to use one of the alternate techniques if you want to, say, do 'sed -n 'f'.
For a long script, it may be more convenient to use a heredoc. An advantage of a heredoc is that you don't need to quote the single quotes inside, if any. A major downside is that the script is fed to sed on its standard input, with two annoying consequences. Some versions of sed require -f /dev/stdin instead of -f -, which is a problem for portability. Worse, the script can't act as a filter, because the standard input is the script and can't be the data.
<code class="language-bash">
exec sed -f - -- "$@" <<'EOF'
sed  shellscripting  shebang  solution  dammitbrain 
january 2018 by kme
PEP 397 -- Python launcher for Windows |
See also:

Basically: put the Python interpreter version in the shebang to specify which version your scripts need, and you can run 'py -2' to get the Python 2.x version if you have both installed.
python  windows  fileassociations  shebang  scripting 
december 2017 by kme
shell - Invoking a script, which has an awk shebang, with parameters (vars) - Stack Overflow |
Using 'env -S' actually works for jq, too (in OS X, anyway).
env is the easiest way to handle this problem:

#!/usr/bin/env -S awk -f

to add more options, and to ensure no interference with your arguments, and awk's arguments:

#!/usr/bin/env -S awk -F: -f ${_} --
# delete argv[1], which == ENVIRON[_]
delete ARGV[1]
} # rest of my awk program

as env has a POSIX standard, this shbang should get you around the difficulties of non-standard shbang implementations across unixen.


after having written this I realized that '-S' is a non-POSIX compliant FreeBSD env extension. So shell wrapper is probably the way to go, unfortunate as that is.
jq  awk  shellscripting  shebang  solution 
october 2017 by kme

Copy this bookmark:

to read