*export* all variables from key=value file to shell

后端 未结 1 1377
自闭症患者
自闭症患者 2021-02-01 18:31

If I want to inherit environment variables to child processes, i do something like:

export MYVAR=tork

Assume I have a a file site.conf

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 18:37

    Run set -a before sourcing the file. This marks all new and modified variables that follow for export automatically.

    set -a
    source site.conf
    set +a  # Require export again, if desired.
    

    The problem you observed is that the pipe executes the export in a subshell. You can avoid that simply by using input redirection instead of a pipe.

    while read assignment; do
      export "$assignment"
    done < site.conf
    

    This won't work, however, if (unlikely though it is) you have multiple assignments on one line, such as

    EMAIL="dev@example.com" FULLNAME="Master Yedi" 
    

    0 讨论(0)
提交回复
热议问题