Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9152695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:07:07+00:00 2026-06-17T12:07:07+00:00

I am trying to write a script which is called from the shebang line

  • 0

I am trying to write a script which is called from the shebang line and returns the right interpreter based on a condition (in my case, based on OS version) and I can’t understand why it doesn’t work.
I’ll try to clarify by an example:
Executable file 1: /home/orens/dynamically_select_interpreter/select_interpreter.sh:

#!/usr/bin/env bash
echoerr() { echo "$@" 1>&2; }

interpreter=`cat /home/orens/dynamically_select_interpreter/interpreter`
if [ -z "$interpreter" ]; then
  echoerr "Could not find interpreter!"
  exit 1
fi

echoerr "Found interpreter: $interpreter"

exec "$interpreter" "$@"

This script selects the interpreter based on some file’s contents (in this case the contents are: /usr/bin/python). Then, it replaces itself with the right interpreter by calling exec.
Calling it from command line, this is what I get:

$ /home/orens/dynamically_select_interpreter/select_interpreter.sh
Found interpreter: /usr/bin/python
Python 2.4.3 (#1, Dec 10 2010, 17:24:35)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

However, when I try to use it inside a shebang line, like this:

#!/home/orens/dynamically_select_interpreter/select_interpreter.sh


echo $SHELL # should be an error # LINE 1
import sys; print "HERE"; print sys.version # this is the actual error # LINE 2

the script is executed as a shell script instead of a python script. LINE 1 is executed properly while LINE 2 raises errors. It looks like the shebang line is silently ignored (and even the print to stderr doesn’t show on screen).

Basically, I am trying to do the same as env, but from a script. I guess that if I write it in C/C++ and compile I’ll get what I want, but since this script will be used as a tool to overcome multiple-kernel-versions situation, I would really like my executable to be OS-independant and my only way to achieve this is by a script.

Can anybody explain this behaviour or help me solve it?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:07:08+00:00Added an answer on June 17, 2026 at 12:07 pm

    Whether the additional redirection works will depend on your OS. Your example (slightly simplified, but the idea stays the same) works on my Debian installation. But some operating systems have limitations in how they interpret the shebang line, including limitations to the length of the command, the number of arguments the command may take etc. Check out this discussion for some examples – they use PERL but essentially try to do the same thing as you. There is a project on sourceforge called shebang-wrapper which might be of help, but I haven’t tested it in practice and it’s still in pre-alpha.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all I am trying to write a script which recursively searches directories from
I'm trying to write a script which checks for prerequisite software in Ubuntu and
I'm trying to write a script which lists a directory and creates an SQL
I am trying to write a script which uploads a file via a html
I'm trying to write a basic script which creates a UI. I have reduced
I'm trying to write a very lightweight build script which will basically just get
I am trying to write a Java program or Hadoop Pig script which will
What I'm trying to do is to write a script which would open an
I am trying to write a simple piece of Jquery in which the script
I'm trying to write a Perl CGI script to handle XML-RPC requests, in which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.