Defining a variable with or without export

后端 未结 14 1464
借酒劲吻你
借酒劲吻你 2020-11-22 09:58

What is export for?

What is the difference between:

export name=value

and

name=value
相关标签:
14条回答
  • 2020-11-22 10:34

    Here's yet another example:

    VARTEST="value of VARTEST" 
    #export VARTEST="value of VARTEST" 
    sudo env | grep -i vartest 
    sudo echo ${SUDO_USER} ${SUDO_UID}:${SUDO_GID} "${VARTEST}" 
    sudo bash -c 'echo ${SUDO_USER} ${SUDO_UID}:${SUDO_GID} "${VARTEST}"'  
    

    Only by using export VARTEST the value of VARTEST is available in sudo bash -c '...'!

    For further examples see:

    • http://mywiki.wooledge.org/SubShell

    • bash-hackers.org/wiki/doku.php/scripting/processtree

    0 讨论(0)
  • 2020-11-22 10:36

    export makes the variable available to sub-processes.

    That is,

    export name=value
    

    means that the variable name is available to any process you run from that shell process. If you want a process to make use of this variable, use export, and run the process from that shell.

    name=value
    

    means the variable scope is restricted to the shell, and is not available to any other process. You would use this for (say) loop variables, temporary variables etc.

    It's important to note that exporting a variable doesn't make it available to parent processes. That is, specifying and exporting a variable in a spawned process doesn't make it available in the process that launched it.

    0 讨论(0)
  • 2020-11-22 10:38

    export NAME=value for settings and variables that have meaning to a subprocess.

    NAME=value for temporary or loop variables private to the current shell process.

    In more detail, export marks the variable name in the environment that copies to a subprocesses and their subprocesses upon creation. No name or value is ever copied back from the subprocess.

    • A common error is to place a space around the equal sign:

      $ export FOO = "bar"  
      bash: export: `=': not a valid identifier
      
    • Only the exported variable (B) is seen by the subprocess:

      $ A="Alice"; export B="Bob"; echo "echo A is \$A. B is \$B" | bash
      A is . B is Bob
      
    • Changes in the subprocess do not change the main shell:

      $ export B="Bob"; echo 'B="Banana"' | bash; echo $B
      Bob
      
    • Variables marked for export have values copied when the subprocess is created:

      $ export B="Bob"; echo '(sleep 30; echo "Subprocess 1 has B=$B")' | bash &
      [1] 3306
      $ B="Banana"; echo '(sleep 30; echo "Subprocess 2 has B=$B")' | bash 
      Subprocess 1 has B=Bob
      Subprocess 2 has B=Banana
      [1]+  Done         echo '(sleep 30; echo "Subprocess 1 has B=$B")' | bash
      
    • Only exported variables become part of the environment (man environ):

       $ ALICE="Alice"; export BOB="Bob"; env | grep "ALICE\|BOB"
       BOB=Bob
      

    So, now it should be as clear as is the summer's sun! Thanks to Brain Agnew, alexp, and William Prusell.

    0 讨论(0)
  • 2020-11-22 10:38

    Although not explicitly mentioned in the discussion, it is NOT necessary to use export when spawning a subshell from inside bash since all the variables are copied into the child process.

    0 讨论(0)
  • 2020-11-22 10:40

    It should be noted that you can export a variable and later change the value. The variable's changed value will be available to child processes. Once export has been set for a variable you must do export -n <var> to remove the property.

    $ K=1
    $ export K
    $ K=2
    $ bash -c 'echo ${K-unset}'
    2
    $ export -n K
    $ bash -c 'echo ${K-unset}'
    unset
    
    0 讨论(0)
  • 2020-11-22 10:40

    The accepted answer implies this, but I'd like to make explicit the connection to shell builtins:

    As mentioned already, export will make a variable available to both the shell and children. If export is not used, the variable will only be available in the shell, and only shell builtins can access it.

    That is,

    tango=3
    env | grep tango # prints nothing, since env is a child process
    set | grep tango # prints tango=3 - "type set" shows `set` is a shell builtin
    
    0 讨论(0)
提交回复
热议问题