I read in another answer that I'm not able to pass arguments to the interpreter than I'm giving to /usr/bin/env
:
Another potential problem is that the
#!/usr/bin/env
trick doesn't let you pass arguments to the interpreter (other than the name of the script, which is passed implicitly).
However, it looks like I am able to, because awk
is breaking when I don't give it the -f
flag and it's fixed when I do give it the -f
flag, while using /usr/bin/env
:
First, without the -f
flag:
$ cat wrap_in_quotes
#!/usr/bin/env awk
# wrap each line in quotes
# usage: wrap_in_quotes [ file ... ]
{ print "\""$0"\"" }
$ echo foobar | ./wrap_in_quotes
awk: syntax error at source line 1
context is
>>> . <<< /wrap_in_quotes
awk: bailing out at source line 1
Second, with the -f
flag:
$ vim wrap_in_quotes
$ cat wrap_in_quotes
#!/usr/bin/env awk -f
# wrap each line in quotes
# usage: wrap_in_quotes [ file ... ]
{ print "\""$0"\"" }
$ echo foobar | ./wrap_in_quotes
"foobar"
- So, if according to the linked answer I'm not able to pass flags to the interpreter, why am I able to pass the
-f
flag toawk
?
I'm running macOS
:
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.12.1
BuildVersion: 16B2657
#!/usr/bin/env
isn't “becoming rather popular”, it's a decade-old de facto standard. I've seen such discussions in the late 1990s, and already at the time the conclusion was that#!/usr/bin/env
was mostly portable but didn't work on a few exotic platforms (SCO, Nextstep). Today Unix platforms without#!/usr/bin/env
are exotic and obsolete. Claiming that “it isn't really all that portable either” is just wrong (unless you're targeting non-unix platforms of course). – Gilles 'SO- stop being evil' Apr 28 '17 at 12:39