Given the command in your example, echo
will run, but what happens to $APPLE
is a little more complicated.
It is true, as is indicated in @Patrick's answer here, that if the shell invokes a process all variables declared on the command-line preceding its invocation are specified to be exported into its environment. And further, those variables are also specified to expire with the invoked process - so...
unset var; var=val cmd; echo ${var-unset.}
...should print unset.
But where this whole concept gets a little more complicated though, and as your own example demonstrates, is at the point that cmd
is not an invoked process but is instead either a shell-builtin, a shell function, or a special shell builtin utility. In every one of those three cases the shell will most likely just run some of its own routines in memory, and invoke nothing at all.
For example, echo
is almost definitely a shell builtin utility - as most shells I know of provide it as such - but it is not a POSIX-specified special builtin. In this way, it is basically a shell function that must emulate an outside executable. This is probably stated a little more clearly here:
The term "built-in" implies that the shell can execute the utility directly and does not need to search for it. An implementation may choose to make any utility a built-in; however, the special built-in utilities described here differ from regular built-in utilities...
Variable assignments specified with special built-in utilities remain in effect after the built-in completes; this shall not be the case with a regular built-in or other utility.
The special built-in utilities in this section need not be provided in a manner accessible via the exec family of functions defined in the System Interfaces volume of POSIX.1-2008.
So a variable declared on echo
's command-line perishes with echo
, but one declared on set
's command-line persists - (though bash
by default violates this rule). The same holds true when cmd
is a shell function:
When a function is executed, it shall have the syntax-error and variable-assignment properties described for special built-in utilities in the enumerated list at the beginning of Special Built-In Utilities.