4 ms·
Incidentally, I've seen some people argue it's more portable to use '#!/usr/bin/env sh' than '#!/bin/sh'. Most every machine I've been on has both /bin/sh and
by smanek 17y ago
Incidentally, I've seen some people argue it's more portable to use '#!/usr/bin/env sh' than '#!/bin/sh'.
Most every machine I've been on has both /bin/sh and /usr/bin/env, so it's a bit of a non-issue: but does anyone know what the 'correct' shebang for a sh script is?
- masklinn 17y agoThe latter, because it simplifies using alternate sh if you need to e.g. create separate execution environments, à la virtualenv in python. In fact that's a good example, though it's in python: if you use `#!/usr/bin/python` as your shebang your script will break under virtualenv, whereas if you use `#!/usr/bin/env python` it will work perfectly. Now /bin/sh is probably a POSIX requirement so it'll always be there and you can always override the shebang with an explicit shell call (or by temporarily rebinding /bin/sh though that one requires root access), but going through env doesn't cost you (as a programmer) more and it simplifies the job of the users (if they need to sandbox your script in some way)
- smanek 17y agoThanks, that makes sense. I suppose I'll use an env shebang from now.
- silentbicycle 17y agoI use /usr/bin/env for most things (Python and Lua, for example), but still use /bin/sh for the bourne shell, because that's part of the standard.
- tptacek 17y agoTcl taught me to "env tclsh" instead of specifying the path, and I do it religously now. You don't want to hardcode the path to the interpreter; people will often install their own, and no matter what the vendor says, Tcl, Python, and Ruby aren't really part of the base system.
- doki_pen 17y agoIt's very difficult to setup a gentoo box without python installed. (maybe impossible?)
- deleted 17y ago[deleted]
- sorbits 17y agoMore portable in that you do not hardcode the path. Ruby, Python, and similar are generally not in the same location across systems. Unfortunately on Ubuntu (and probably other Linux variants) you can’t pass arguments to the interpreter, e.g. the following gives an error on Ubuntu (works on OpenSolaris, FreeBSD, and OS X): #!/usr/bin/env ruby -wKU puts "æblegrød"