tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Relative interpreter pathname on #! line



Is the following interpretation the shebang line intentional?

$ mkdir example
$ cd example

### Starting the interpreter with ./ works:

$ printf '#! ./sh \n echo it works \n' >example.sh
$ chmod +x example.sh
$ ./example.sh
ksh: ./example.sh: No such file or directory
$ ln -s /bin/sh
$ ./example.sh
it works

### Even the basename without the ./ prefix works:

$ printf '#! sh \n echo it works \n' >example2.sh
$ chmod +x example2.sh
$ ./example2.sh
it works
$ rm sh
$ ./example2.sh
ksh: ./example2.sh: No such file or directory

### A blank "#!" line seems to somehow assume sh (or $SHELL?)

$ printf '#! \n echo it works \n' >example3.sh
$ chmod +x example3.sh
$ ./example3.sh
it works

What's more, the relative interpreter filename is taken relative to the current directory, not relative to the script directory.

Linux has the same behavior.

There's probably some logic to this but it seems bug-prone and I can't think of a legitimate use for it.

Home | Main Index | Thread Index | Old Index