On Unix-like operating systems, the interpreter to start can be specified by the first line (called shebang) starting with #!
In the script. The following is an example borrowed from Wikipedia.
example1.sh
#! /bin/sh
echo 'Hello world!'
This shebang has various troubles such as restrictions and different interpretations depending on the OS.
-Bookworm: About the mysterious de facto industry standard Shebang
That aside, to launch Ruby,
example1.rb
#! /usr/bin/ruby
puts 'Hello world!'
However, Ruby is not always in / usr / bin / ruby
. Depending on the system, it may be in / usr / local / bin / ruby
. The ʻenv` command is often used to avoid this problem.
example2.rb
#! /usr/bin/env ruby
puts 'Hello world!'
This seems to work in most environments, but there are operating systems out there that don't have / usr / bin / env
. (Shohei Urabe doesn't reblog much tumblr: #! / usr / bin / env)
In conclusion, it seems good to write like this. (Ruby 1.8.7 Reference Manual: Starting Ruby)
example3.rb
#! /bin/sh
exec ruby -S -x "$0" "$@"
#! ruby
The started sh
jumps to Ruby with ʻexec` on the second line, and Ruby skips up to the third line.
Do the same thing in Python: (effbot.org: How do I make a Python script executable on Unix?)
example.py
#! /bin/sh
""":"
exec python "$0" ${1+"$@"}
"""
__doc__ = """The above defines the script's __doc__ string. You can fix it by like this."""
On the other hand, Perl users are like this. (The Magic Perl Header)
example.pl
#! /bin/sh
eval '(exit $?0)' && eval 'PERL_BADLANG=x;PATH="$PATH:.";export PERL_BADLANG\
;exec perl -x -S -- "$0" ${1+"$@"};#'if 0;eval 'setenv PERL_BADLANG x\
;setenv PATH "$PATH":.;exec perl -x -S -- "$0" $argv:q;#'.q
#!perl -w
+push@INC,'.';$0=~/(.*)/s;do(index($1,"/")<0?"./$1":$1);die$@if$@__END__+if 0
;#Don't touch/remove lines 1--7: http://www.inf.bme.hu/~pts/Magic.Perl.Header
** Addendum (2015/10/21) **
The Python example above seems to get stuck in pep257. So I tried to get past this check.
example2.py
#!/bin/sh
""":" .
exec python "$0" "$@"
"""
__doc__ = """
The above defines the script's __doc__ string. You can fix it by like this."""
Recommended Posts