I'm trying to figure out exactly what the semantics of the shebang are.
I can write a script like this:
#!/usr/bin/env bash
if [ -z "$FOO" ]
then
echo "No FOO"
else
echo "$FOO"
fi
without $FOO
in my environment, and run it like ./foo.sh
, bash foo.sh
, env bash foo.sh
, etc and it will print "No FOO" as expected.
I can of course run it like FOO=bar ./foo.sh
and it will print bar
.
The env
man page gives the invocation as:
env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
And I can use it as such:
$ env FOO=bar bash foo.sh
bar
However, if I try to use that syntax in a shebang:
#!/usr/bin/env FOO=bar bash
if [ -z "$FOO" ]
then
echo "No FOO"
else
echo "$FOO"
fi
then ./foo.sh
hangs indefinitely and does not execute.
Can someone explain this to me? I assumed that when a shebang is encountered, it just copies the command, appends the path to the script to the end of the argument list, and executes it, but this behavior suggests otherwise.