Live data from Hacker News

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

utcc.utoronto.ca

21–30 of 116 posts

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

#21

Earlier quoted context omitted.

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

And is this shebang guaranteed to work always? Why isn't it more common?

It’s quite common, although I probably see it used more frequently to invoke other (non-shell) scripting languages.

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

#22

Earlier quoted context omitted.

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

And is this shebang guaranteed to work always? Why isn't it more common?

It's guaranteed to work provided that Bash is in the path.

It's very common for Python. Less so for Bash for two reasons: because the person who writes the script references /bin/sh instead (which is required to be there) even when they are writing bash-isms, or because the person who writes the script assumes that Bash is universally available as /bin/bash.

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

#23

Earlier quoted context omitted.

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

And is this shebang guaranteed to work always? Why isn't it more common?

Because /bin is the standard location for bash. The only one that breaks that expectation is NixOS (and maybe GuixSD?), apparently. I'm surprised they didn't symlink /bin or put a stub. Last time I tried NixOS was like 10 years ago. I thought there was a /bin/bash, but maybe it was just a /bin/sh?

Other interpreters like python, ruby, etc. have more likelyhood of being used with "virtual environments", so it's more common to use /usr/bin/env with them.

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

#25
post #23

Earlier quoted context omitted.

And is this shebang guaranteed to work always? Why isn't it more common?

Because /bin is the standard location for bash. The only one that breaks that expectation is NixOS (and maybe GuixSD?), apparently. I'm surprised they didn't symlink /bin or put a stub. Last time I tried NixOS was like 10 years ago. I thought there was a /bin/bash, but maybe it was just a /bin/sh? Other interpreters like python, ruby, etc. have more likelyhood of being used with "virtual environments", so it's more c…

They do symlink /bin/sh to be fair, and that's very often good enough for a lot of scripts. That's what I usually do if I don't need anything bash offers.

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

#27
post #18
post #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 t…

Or, like, if you aren't reading and caring about what the interpreter is--as that's the only time this can burn you: it isn't doing a PATH lookup, so you can't walk into this one on accident--then it could literally be something like /bin/rm on some key file. This entire article is based on an assumption that this is somehow so obviously bad that there doesn't even need to be an explanation or defense of any kind of…

> This entire article is based on an assumption that this is somehow so obviously bad that there doesn't even need to be an explanation or defense of any kind of that idea.

I'm not reading it like that. The tone is just one of surprise, since this isn't something that one typically sees. Since it's obscure, it leads one to wonder if it can be bad, and I don't see how it could be.

I think it survived in the independent Linux because it's the simple and obvious way to do things, and it doesn't lead to any exceptional power of misuse one didn't already have with writing the rest of the file.

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

#28
post #25
post #23

Earlier quoted context omitted.

Because /bin is the standard location for bash. The only one that breaks that expectation is NixOS (and maybe GuixSD?), apparently. I'm surprised they didn't symlink /bin or put a stub. Last time I tried NixOS was like 10 years ago. I thought there was a /bin/bash, but maybe it was just a /bin/sh? Other interpreters like python, ruby, etc. have more likelyhood of being used with "virtual environments", so it's more c…

They do symlink /bin/sh to be fair, and that's very often good enough for a lot of scripts. That's what I usually do if I don't need anything bash offers.

Thing is, a few years ago when Debian changed its default sh from bash to ... either ash or dash, I forget which, I got into the habit of always writing `#!/bin/bash` at the top of my scripts, in case I didn't realize that something I was using was a bashism not found in classic /bin/sh. So if I used Nix (I don't, since for my particular use cases the juice isn't worth the squeeze), I would get seriously messed up by that.

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

#29
The thing that surprised me was that you can't write an interpreter in an interpreted language, at least not in obsd. It is possible if you jump through a few hoops but you can't directly call it.

An example: if you made a language in python /bin/my_lang: #does nothing but pretend it does

    #!/usr/local/bin/python3
    import sys
    print('my_lang args', sys.argv)
    for line in sys.stdin:
      print('invalid_line:', line)
my_script:

    #!/bin/my_lang
    line of stuff
    another line of stuff

    chmod u+x my_script
    ./my_script
Probably for the best, but I was a bit sad that my recursive interpreter scheme was not going to work.

Update: looks like linux does allow nested interpreters, good for them.

https://www.in-ulm.de/~mascheck/various/shebang/#interpreter...

really that whole document is a delightful read.

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

#30
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…

> apparently I can just have done `#!bash`

I think you're mixing two concepts: relative paths (which are allowed after #! but not very useful at all) and file lookup through $PATH (which is not done by the kernel, maybe it's some shell trickery).

Post reply on HN