pass bash variable as argument to gcc

Jeff Epler jepler@unpythonic.net
Fri Oct 30 02:44:00 GMT 2015


It is useful to understand how the shell behaves.  The common bash shell
is intended to be a superset of the standard Unix shell, which is
described in this standards document
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
-- though by web search, you will be able to find other explanations of
the unix shell which may be easier to read and understand.

Anyhow, we are interested in how simple commands are parsed and
executed, in order to understand why your original alternative did not
work.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01

Notice the following statement about a simple command that has both a
variable assignment and a command name:

    If no command name results, or if the command name is a special built-in
    or function, 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 as a side-effect of the expansions
    performed in step 4. In this case it is unspecified:
        (emphasis mine)

This requirement of the standard is why your original command did not
work as you desired.

Here are some simple uses of 'echo' that show several cases of how shell
variables and environment variables behave -- the last one is
particularly surprising to someone not familiar with the
rules of the Unix shell:

$ unset unsetvar
$ echo "${unsetvar-unset}"
unset
$ var="x y z"
$ echo "${var-unset}"
x y z
$ unset ENVVAR
$ ENVVAR="x y z" echo "${ENVVAR-unset}"
unset
$ ENVVAR="x y z"; export ENVVAR
$ ENVVAR="a b c" echo "${ENVVAR-unset}"
x y z

Jeff



More information about the Gcc-help mailing list