Setting an environment variable on same line as program execution is different than setting it separately? - shell variables vs. environment variables

后端 未结 2 1123
南方客
南方客 2020-12-14 16:45

What is the difference between:

prompt$   TSAN_OPTIONS=\"suppressions=/somewhere/file\" ./myprogram

and

prompt$   TSAN_OPTI         


        
2条回答
  •  一个人的身影
    2020-12-14 17:35

    The format VAR=value command sets the variable VAR to have the value value in the environment of the command command. The spec section covering this is the Simple Commands. Specifically:

    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.

    The format VAR=value; command sets the shell variable VAR in the current shell and then runs command as a child process. The child process doesn't know anything about the variables set in the shell process.

    The mechanism by which a process exports (hint hint) a variable to be seen by child processes is by setting them in its environment before running the child process. The shell built-in which does this is export. This is why you often see export VAR=value and VAR=value; export VAR.

    The syntax you are discussing is a short-form for something akin to:

    VAR=value
    export VAR
    command
    unset -v VAR
    

    only without using the current process environment at all.

提交回复
热议问题