by _arvin · 8y ago · 0 comments
#!/bin/bash
as opposed to:
#!/usr/bin/env bash
the latter feels more flexible and dependable for a script to be passed around.
People who share code for the world to use should use:
- `#!/usr/bin/env bash`
- or `#!/bin/sh` for POSIX shell scripting
edit: bourne again shell
Never once have I ever seen this. In 25+ years.
- it works
- it's simple
- they know that bash is in /bin
- maybe it avoids a needless call to env, resulting in better startup time (see e.g. https://news.ycombinator.com/item?id=16978932 for a similar situation with python)
Why is it better than the other statement?
Thanks!
/bin/bash breaks on FreeBSD, which installs Bash at /usr/local/bin/bash. /usr/bin/env bash works though.
> You also can't pass command line arguments.
Are you sure about that? Given this script:
#!/usr/bin/env bash
echo "args: $@"
it outputs: $ ./foo 1 2 3
args: 1 2 3
Is that what you meant?Good point about freebsd - I remember running into that exact problem. But I think I just symlinked it.
https://unix.stackexchange.com/questions/29608/why-is-it-bet... has more info on the pitfalls.
In my experience env causes problems, whereas explicit binary call always works (assuming the path exists). Not sure why I was downvoted.
Makes sense. Thanks for clarifying. I had a feeling I wasn't understanding your point.
#!/usr/bin/env bash -ex
but you can: #!/bin/bash -exIts not a ruby thing, its the way you're supposed to write portable scripts where the location of the executable might not be predetermined. Lets say you're on a distribution where bash is in /usr/bin/bash instead of /bin/bash. Well you'll be modifying the shebang line to either do /usr/bin/env bash or pointing it to that spot.
What old systems didn't it work on? I've used everything from SunOS, Irix, HPUX, AiX, Solaris, Linux, bsd's, anything claiming to be posix has to support that.
/usr/bin/env is not POSIX anymore than /bin/bash etc.
Mentions that the env command is POSIX, but it doesn't mention the absolute path. Are you saying that /use/bin/env is not POSIX because there is no guarantee that it will be available at that particular path?
I run into this trying to run rvm-managed Ruby scripts in cronjobs. The solution (IIRC) is to explicitly reference the interpreter you want to run in the cronjob line.