Live data from Hacker News

A surprise with how '#!' handles its program argument in practice

utcc.utoronto.ca

1–10 of 116 posts

Re: A surprise with how '#!' handles its program argument in practice

#2
Huh. I wish I had known this before.

NixOS is annoying because everything is weird and symlinked and so I find myself fairly frequently making the mistake of writing `#!/bin/bash`, only to be told it can't find it, and I have to replace the path with `/run/current-system/sw/bin/bash`.

Or at least I thought I did; apparently I can just have done `#!bash`. I just tested this, and it worked fine. You learn something new every day I guess.

Re: A surprise with how '#!' handles its program argument in practice

#3
post #2

Huh. I wish I had known this before. NixOS is annoying because everything is weird and symlinked and so I find myself fairly frequently making the mistake of writing `#!/bin/bash`, only to be told it can't find it, and I have to replace the path with `/run/current-system/sw/bin/bash`. Or at least I thought I did; apparently I can just have done `#!bash`. I just tested this, and it worked fine. You learn something new…

Seems like it only works in zsh, not bash or fish

Re: A surprise with how '#!' handles its program argument in practice

#4
> Although this is probably the easiest way to implement '#!' inside the kernel, I'm a little bit surprised that it survived in Linux (in a completely independent implementation) and in OpenBSD (where the security people might have had a double-take at some point). But given Hyrum's Law there are probably people out there who are depending on this behavior so we're now stuck with it.

I don't see what there would be to gain in disallowing the program path on the shebang line to be relative. The person that wrote the shebang can also write relative paths in some other part of the file.

Re: A surprise with how '#!' handles its program argument in practice

#5
post #3
post #2

Huh. I wish I had known this before. NixOS is annoying because everything is weird and symlinked and so I find myself fairly frequently making the mistake of writing `#!/bin/bash`, only to be told it can't find it, and I have to replace the path with `/run/current-system/sw/bin/bash`. Or at least I thought I did; apparently I can just have done `#!bash`. I just tested this, and it worked fine. You learn something new…

Seems like it only works in zsh, not bash or fish

  [tombert@puter:~/testscript]$ ./myscript.sh
    bash: ./myscript.sh: bash: bad interpreter: No such file or directory
You are right. Appears to only work with zsh. I will resume being annoyed then.

Re: A surprise with how '#!' handles its program argument in practice

#6
post #2

Huh. I wish I had known this before. NixOS is annoying because everything is weird and symlinked and so I find myself fairly frequently making the mistake of writing `#!/bin/bash`, only to be told it can't find it, and I have to replace the path with `/run/current-system/sw/bin/bash`. Or at least I thought I did; apparently I can just have done `#!bash`. I just tested this, and it worked fine. You learn something new…

`#!/usr/bin/env bash` is the most portable form for executing it from $PATH

Re: A surprise with how '#!' handles its program argument in practice

#7
post #2

Huh. I wish I had known this before. NixOS is annoying because everything is weird and symlinked and so I find myself fairly frequently making the mistake of writing `#!/bin/bash`, only to be told it can't find it, and I have to replace the path with `/run/current-system/sw/bin/bash`. Or at least I thought I did; apparently I can just have done `#!bash`. I just tested this, and it worked fine. You learn something new…

Anything other than ”#!/usr/bin/env bash” is doomed to fail at some time.

Re: A surprise with how '#!' handles its program argument in practice

#8
post #3
post #2

Huh. I wish I had known this before. NixOS is annoying because everything is weird and symlinked and so I find myself fairly frequently making the mistake of writing `#!/bin/bash`, only to be told it can't find it, and I have to replace the path with `/run/current-system/sw/bin/bash`. Or at least I thought I did; apparently I can just have done `#!bash`. I just tested this, and it worked fine. You learn something new…

Seems like it only works in zsh, not bash or fish

The kernel interprets the shebang line, not the shell.

Re: A surprise with how '#!' handles its program argument in practice

#9
post #5
post #3

Earlier quoted context omitted.

Seems like it only works in zsh, not bash or fish

[tombert@puter:~/testscript]$ ./myscript.sh bash: ./myscript.sh: bash: bad interpreter: No such file or directory You are right. Appears to only work with zsh. I will resume being annoyed then.

Is this UNIX?

Re: A surprise with how '#!' handles its program argument in practice

#10
post #5

Earlier quoted context omitted.

[tombert@puter:~/testscript]$ ./myscript.sh bash: ./myscript.sh: bash: bad interpreter: No such file or directory You are right. Appears to only work with zsh. I will resume being annoyed then.

Is this UNIX?

This is NixOS, so no, it's Linux. I guess I just hoped it would work on Linux as well.
Post reply on HN