{"thread":{"id":"305","subject":"Why /usr/bin/env in scripts","startedAt":"2005-04-25T07:38:11Z","lastAt":"2005-04-25T17:45:19Z","messageCount":3,"participants":["Philip Pokorny","David Greaves","David A. Wheeler"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"1600","messageId":"426C9E63.4050907@mindspring.com","threadId":"305","inReplyTo":null,"subject":"Why /usr/bin/env in scripts","fromName":"Philip Pokorny","fromEmail":"ppokorny@mindspring.com","sentAt":"2005-04-25T07:38:11Z","receivedAt":"2005-04-25T07:38:11Z","isPatch":false,"sender":{"key":"ppokorny@mindspring.com","avatar":null},"body":"I notice that the first line of the pasky shell scripts is\n\n#!/usr/bin/env bash\n\nrather than what I'm more familiar with:\n\n#!/bin/bash\n\nI see that env can be used to alter the environment before running a \nprogram, but that doesn't seem to be happening here.\n\nIn fact, on my system /usr/bin/env is a link to /bin/env...\n\nSo what am I missing?  Is this a portability aid?\n\nThanks,\n:v)\n"},{"id":"1603","messageId":"426CBB95.9040203@dgreaves.com","threadId":"305","inReplyTo":"426C9E63.4050907@mindspring.com","subject":"Re: Why /usr/bin/env in scripts","fromName":"David Greaves","fromEmail":"david@dgreaves.com","sentAt":"2005-04-25T09:42:45Z","receivedAt":"2005-04-25T09:42:45Z","isPatch":false,"sender":{"key":"david@dgreaves.com","avatar":"https://gravatar.com/avatar/ca67bad50999edcdd137c9a65da2381557d175bea99ae956afdabc5785e42b79?d=mp&s=160"},"body":"Philip Pokorny wrote:\n> I notice that the first line of the pasky shell scripts is\n> \n> #!/usr/bin/env bash\n\n> So what am I missing?  Is this a portability aid?\nman env:\n\nThe first remaining argument specifies the program name to invoke;\nit is searched for according to the `PATH' environment variable.  Any\nremaining arguments are passed as arguments to that program.\n\nIt's a trick to do a PATH search for a script interpreter rather than \nhard coding the path.\nNow all we have to do is make sure every distro on the planet has \n/usr/bin/env - and they do.\n\nDavid\n"},{"id":"1630","messageId":"426D2CAF.7080101@dwheeler.com","threadId":"305","inReplyTo":"426C9E63.4050907@mindspring.com","subject":"Re: Why /usr/bin/env in scripts","fromName":"David A. Wheeler","fromEmail":"dwheeler@dwheeler.com","sentAt":"2005-04-25T17:45:19Z","receivedAt":"2005-04-25T17:45:19Z","isPatch":false,"sender":{"key":"dwheeler@dwheeler.com","avatar":"https://avatars.githubusercontent.com/u/813150?v=4"},"body":"Philip Pokorny wrote:\n\n> I notice that the first line of the pasky shell scripts is\n>\n> #!/usr/bin/env bash\n>\n> rather than what I'm more familiar with:\n>\n> #!/bin/bash\n\n...\n\n> So what am I missing?  Is this a portability aid?\n\n\nCorrect.  Bash gets installed in different places on different systems,\ne.g., on OpenBSD bash is NOT in /bin or /usr/bin, but in /usr/local/bin.\nInvoking \"/bin/sh\" on OpenBSD will _NOT_ get you bash, but a pdksh\nvariant that doesn't implement the bash capabilities some scripts require.\nUsing env this way an old trick; I don't know who originally came up \nwith it.\n\nTechnically, even this isn't 100% portable.  Leading \"#!\" (shebang)\nisn't implemented by a very few old systems (it's not in the Single User \nSpec,\nthough it was introduced between v7 and v8 of Unix). Also,\nyou can find one or two really obscure systems where env isn't in /usr/bin\n(OpenServer 5 and a few really old Linux distributions).\nBut env is ONLY in /usr/bin for the *BSDs.\nThis version runs on 99.999% of the Linux/Unix systems, and takes\nessentially zero effort to implement; \"more portable\" solutions are so much\nmore work that few people bother if they're writing shell scripts.\n\n--- David A. Wheeler\n\n"}]}