1

I'm trying to implement, in shell, a script interpreter that I can use in a shebang line. For example, here is my interpreter and a test script using that interpreter:

-----
$ cat interp
#! /usr/bin/env bash

printf "interp called with args: %s\n" "$*"

-----
$ cat test
#! ./interp

printf "test script!\n"

When I run ./test on a Linux system, interp is called with the arguments that I expect:

$ ./test -a
interp called with args: ./test -a

But when I do the same on Mac OS X 10.8.4, I get the following result:

$ ./test -a
test script!

The contents of the test script are being executed by bash, rather than the interp script being executed with the test file as the first argument.

Am I doing this correctly and such a thing simply won't work on Mac OS X? Or is there a better way to accomplish this?

2
  • Works as expected with zsh on OSX 10.6.8, but not with the macports bash. Commented Jul 28, 2014 at 14:08
  • 1
    Mac OS X requires the program named in the shebang to be a binary executable, not a script, AFAICR. The workaround anubhava suggests runs /usr/bin/env (a binary executable) and requests it to run your script. Commented Jul 28, 2014 at 14:14

2 Answers 2

2

Yes there is a way to make it work on OSX, which is same as what you did in your first script i.e. by using /usr/bin/env.

Have ./test as:

#!/usr/bin/env ./interp

printf "test script!\n"
Sign up to request clarification or add additional context in comments.

2 Comments

This does work, than you. Any idea why my first attempt did not work? (It also fails if I use absolute path rather than #! ./interp)
Yes it doesn't work with full path to interp also on OSX. I am still researching why OSX needs /usr/bin/env but Linux doesn't. I will update once I find something.
-1

OS X is built on top of UNIX. The application Terminal takes you from the outer world of OS X to the inner world of UNIX. Terminal is located in the Utilities folder inside the Applications folder. To open Terminal, double-click the Terminal icon, and you see a window like the one shown in the figure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.