|
|
Subscribe / Log in / New account

The case of the supersized shebang

The case of the supersized shebang

Posted Feb 19, 2019 4:34 UTC (Tue) by anguslees (subscriber, #7131)
In reply to: The case of the supersized shebang by pabs
Parent article: The case of the supersized shebang

It's much less common with the "modern" scripting languages (python/ruby), since they're usually treated as full-featured programming environments quite separate to any functionality they have as an interactive shell experience.

Guile is pretty close, with a "\" "meta switch" and defining #! ... !# as comment delimiters. You can do this:

#!/usr/local/bin/guile \
-e main -s
!#
(define (main args)
  (display "hello, world!")
  (newline))
Related, but different:

Many other "scripting" languages use # as a comment character and can do some similar shell bootstrap hack too. Most of these are used to lookup the language interpreter in the path, knowing only that /bin/sh exists.

For some reason I never understood, the modern world has decided it is better to assume that /usr/bin/env exists instead (using env in this way still doesn't allow multiple args, unless you're using freebsd's env -S magic arg - also in coreutils 8.30 apparently).

Perl:

#!/bin/sh
#! -*-perl-*-
eval 'exec perl -x -wS $0 ${1+"$@"}'
    if 0;
# perl code continues here

Python:

#!/bin/sh
''':'
if type python2 >/dev/null 2>/dev/null; then
  exec python2 "$0" "$@"
else
  exec python "$0" "$@"
fi
'''
# Python code continues here
(and similar hacks for awk, ruby, etc)


to post comments

The case of the supersized shebang

Posted Feb 19, 2019 10:14 UTC (Tue) by dottedmag (subscriber, #18590) [Link] (4 responses)

/usr/bin/env is always there, unlike other interpreters which may end up in /usr/bin, /usr/local/bin or, say, /opt/bin, depending on what the user installed on their machine (e.g. macOS MacPorts vs. Homebrew).

The case of the supersized shebang

Posted Feb 19, 2019 11:08 UTC (Tue) by Baughn (subscriber, #124425) [Link]

Even NixOS has /use/bin/env, as the only file in /usr.

The case of the supersized shebang

Posted Feb 19, 2019 13:58 UTC (Tue) by DrHyde (guest, #130508) [Link] (2 responses)

/usr/bin/env is *not* always there. Some older or more obscure platforms have it elsewhere. IIRC I've run across this problem on SunOS and Unicos.

The case of the supersized shebang

Posted Feb 19, 2019 16:01 UTC (Tue) by dbaker (guest, #89236) [Link] (1 responses)

One of my projects has some interesting hacks because Haiku has /bin/env (no /usr at all, IIRC)

The case of the supersized shebang

Posted Feb 19, 2019 17:21 UTC (Tue) by DrHyde (guest, #130508) [Link]

I believe that http://catb.org/jargon/html/V/vaxocentrism.html applies, except with modern Linux instead of VAX.

The case of the supersized shebang

Posted Feb 19, 2019 13:23 UTC (Tue) by smurf (subscriber, #17840) [Link] (2 responses)

Those hacks are (a) wasteful (an extra interpreter needs to start up) and (b) fragile: Python, for instance, requires "from __future__ import …" statements to be the first statement in a file. Owch. "ln -s /bin/true /usr/bin/from", anybody?

The case of the supersized shebang

Posted Feb 19, 2019 16:16 UTC (Tue) by tux3 (subscriber, #101245) [Link] (1 responses)

Experimentaly "from __future import ..." seems to work fine with a #!, or even random comments before.
It seems python is happy as long as it's the first line actually executed, though the error message is confusing.

The case of the supersized shebang

Posted Feb 19, 2019 16:17 UTC (Tue) by tux3 (subscriber, #101245) [Link]

Oh I just realized what message you were replying to, my mistake.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds