Can a script be used as an interpreter by the #! hashbang line?

Viewed 1671

I'm trying to write a bash script which will behave as a basic interpreter, but it doesn't seem to work: The custom interpreter doesn't appear to be invoked. What am I doing wrong?

Here's a simple setup illustrating the problem:

/bin/interpreter: [owned by root; executable]

#!/bin/bash

echo "I am an interpreter running " $1

/Users/zeph/script is owned by me, and is executable:

#!/bin/interpreter

Here are some commands for the custom interpreter.

From what I understand about the mechanics of hashbangs, the script should be executable as follows:

$ ./script
I am an interpreter running ./script

But this doesn't work. Instead the following happens:

$ ./script 
./script: line 3: Here: command not found

...It appears that /bin/bash is trying to interpret the contents of ./script. What am I doing wrong?

Note: Although it appears that /bin/interpreter never invoked, I do get an error if it doesn't exist:

$ ./script
-bash: ./script: /bin/interpreter: bad interpreter: No such file or directory

(Second note: If it makes any difference, I'm doing this on MacOS X).

4 Answers
Related