Call the right executable on *nix

January 12, 2017 · 1 minute to read · bash, Linux

One of several problems that occurs when you develop scripts is the selection of the right executable, for example usually with python, you can have three executables: python2, python3 and python (which points to one of previous). The problem happens when you have to run your script:

  • by using executable name on command line python ./my_script.py
  • or with a shebang #!/usr/bin/python

With the last one, interpreter does not check the PATH variable. Consequently, shebang needs to point to the right executable independently of underlying system. But on different Linux distributions, python can change from version 2 to version 3. And how to deal with virtualenv?

Right solution must be to use PATH variable like in interpreter to find the first matching executable. Fortunately, there is always a good solution on Linux. Here, it is the env command:

#!/usr/bin/env python

So please, always use this when you will have to run scripts. It can save a lot of time for just a version problem.