How to write the correct shebang in Perl, Python and Ruby scripts

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

How to write the correct shebang in Perl, Python and Ruby scripts
How to write Ruby to_s in Python
Difference in how to write if statement between ruby ​​and python
How to execute external shell scripts and commands in python
[Note] How to write QR code and description in the same image with python
How to package and distribute Python scripts
How to write this process in Perl?
How to use is and == in Python
Offline real-time how to write E11 ruby and python implementation example
The 15th offline real-time how to write reference problem in Python
How to use functions in separate files Perl and Python versions
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
How to display bytes in the same way in Java and Python
How to use the C library in Python
How to generate permutations in Python and C ++
How to get the date and time difference in seconds with python
The trick to write flatten concisely in python
How to get the files in the [Python] folder
How to write async and await in Vue.js
How to plot autocorrelation and partial autocorrelation in python
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
20th Offline Real-time How to Write Problems in Python
How to retrieve the nth largest value in Python
How to get the variable name itself in python
How to get the number of digits in Python
How to write string concatenation in multiple lines in Python
How to know the current directory in Python in Blender
Write tests in Python to profile and check coverage
[Python] How to sort dict in list and instance in list
I want to write in Python! (3) Utilize the mock
How to use the model learned in Lobe in Python
[Python] How to output the list values in order
How to handle JSON in Ruby, Python, JavaScript, PHP
How to develop in Python
How to write custom validations in the Django REST Framework
[python] How to check if the Key exists in the dictionary
How to debug the Python standard library in Visual Studio
How to swap elements in an array in Python, and how to reverse an array.
How to use the __call__ method in a Python class
[Python] How to write an if statement in one sentence.
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
[Blender] How to handle mouse and keyboard events in Blender scripts
How to write a metaclass that supports both python2 and python3
Ruby vs Python Performance. How to pick the right technology?
How to log in to AtCoder with Python and submit automatically
How to do "Lending and borrowing JS methods (the one who uses apply)" in Python
How to write a Python class
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
How to get the Python version
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
[Python] How to import the library
How to use Mysql in python