Given a little bit of discussion last night surrounding the following statements:<div><br></div><div>VAR=VAL; for i in 1 2 3; do echo "$VAR"; done</div><div>VAR=VAL for i in 1 2 3; do echo "$VAR"; done</div>
<div><br></div><div>I decided to take Jan's advice and "test all the things" in bash, dash and FreeBSD sh:</div><div><br></div><div>bash $ VAR=VAL for i in 1 2 3; do echo "$VAR"; done</div><div>-bash: syntax error near unexpected token `do'</div>
<div>dash $ VAR=VAL for i in 1 2 3; do echo "$VAR"; done</div><div>/bin/sh: for: not found</div><div>freebsd sh$ VAR=VAL for i in 1 2 3; do echo "$VAR"; done</div><div>Syntax error: "do" unexpected</div>
<div><br></div><div>None of them seem to work, so I took a look at the POSIX 1.2008 Shell Command Language Specification for Simple Commands (<a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01">http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01</a>), which says that the ``command'' lookup following an optional variable assignment is subject to the rules governing ``Command Search and Execution'' (<a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01">http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01</a>). The rules governing this search (without a slash) starts with ``Special Builtins'', then functions, then moves to a few utilities that will always execute even if they are not in PATH, followed by a PATH search. When we look at the list of ``Special Builtins'', we will see that ``for'' is not there (<a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14">http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14</a>). The reason for this is that ``for'' is a reserved word (<a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_04">http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_04</a>), which just means that for is a part of the grammar of shell itself, and is not a command.</div>
<div><br></div><div>So, assuming that we do have a special built-in, let's say eval ...</div><div><br></div><div>bash $ VAR=VAL eval echo \$VAR</div><div>VAL</div><div>dash $ VAR=VAL eval echo \$VAR</div><div>VAL</div>
<div>freebsd sh$ VAR=VAL eval echo \$VAR</div><div>VAL</div><div><br></div><div>All of our shells do support the optional variable assignment syntax for simple commands, as required by the spec. So now to the second question from last night, does optional variable assignment effect the environment of the shell itself, when combined with a special built-in, or do they only effect the environment of the shell itself for the duration of the execution of the special built-in ...</div>
<div><br></div><div>bash $ VAR=VAL eval echo \$VAR</div><div>VAL</div><div>bash $ echo $VAR</div><div><br></div><div>bash $</div><div>dash $ VAR=VAL eval echo \$VAR</div><div>VAL</div><div>dash $ echo $VAR</div><div>VAL</div>
<div>freebsd sh $ echo $VAR</div><div>VAL</div><div><br></div><div>So, the answer here is that it depends on the shell ... and that is largely because the POSIX standard is mostly silent about this:</div><div><br></div><div>
<span class="Apple-style-span" style="font-family:Verdana,Arial,Helvetica,sans-serif;font-size:13px">If no command name results, variable assignments shall affect the current execution environment. Otherwise, the variable assignments shall be exported for the execution environment of the command and shall not affect the current execution environment (except for special built-ins).</span></div>
<div><span class="Apple-style-span" style="font-family:Verdana,Arial,Helvetica,sans-serif;font-size:13px"><br></span></div><div><span class="Apple-style-span" style="font-family:Verdana,Arial,Helvetica,sans-serif;font-size:13px">I believe that either the behavior of bash here is incorrect, or that either behavior is permissible. But it should be assumed that when using a special built-in with optional variable assignment on the command line, that the variable value may persist in the current execution environment.</span></div>
<div><br></div><div>-- <br>regards,<br>matt<br>
</div>