/usr/bin/env: zsh -: No such file or directory

envscriptingshebangzsh

I get the error

/usr/bin/env: zsh -: No such file or directory

…when I run an executable zsh script that starts with the following shebang line:

#!/usr/bin/env zsh -

Also, FWIW, replacing - with -- causes /usr/bin/env to print a similar complaint about zsh --.

I have only seen this error under ubuntu, and only in the context of the shebang hack. Under darwin the same script runs fine. And under ubuntu, running

% /usr/bin/env zsh -

from the command line succeeds. (Actually, "under ubuntu" should be understood as short-hand for "under ubuntu 12.04 LTS and env (GNU coreutils) 8.13".)

My question is: how can I modify the shebang above to avoid this error?

Of course, I know that removing the trailing - will eliminate the error, but this is not an acceptable solution. The rest of this post explains why.

The shebang line that is causing the error comes about as an attempt to comply with two entirely independent guidelines:

  1. To make scripts portable, use #!/usr/bin/env <cmd ...> rather than #!/path/to/cmd <...>.

  2. Putting - as the sole argument to zsh in the shebang line of zsh scripts thwarts certain types of attacks.

So, I can restate my question more precisely as follows: Is it possible to satisfy both of these guidelines without triggering the error shown above under ubuntu?

Best Answer

Linux (you mentioned "only under Ubuntu" but the only OS you mentioned it working under was Darwin) does not support passing multiple arguments to a 'shebang' interpreter. It passes the entire string (in your case, "zsh -") as a single argument.

The correct way to ensure your package does not depend on the location of an interpreter is to, as part of the installation process, locate the interpreter and modify the script to include the correct path. Some example code to do this (for sh) is provided at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html#tag_20_117_16 .

Related Question