Export variables defined in another file

后端 未结 3 1173
遇见更好的自我
遇见更好的自我 2020-12-30 04:58

I have a script that contains a couple of variables that need to be set as an environment variable

The list of variables change constantly and modifying it on my end

3条回答
  •  悲&欢浪女
    2020-12-30 05:19

    If you are looking to export only variables with a common prefix, e.g.:

    BAZ_FOO="FOOFOO"
    BAZ_BAR="BARBAR"
    

    You could simply do this:

    . foo.sh
    variable_list=()
    variable_list=( $(set -o posix ; set  |\
                      grep "BAZ_"         |\
                      cut -d= -f1) )
    for variable in "${variable_list[@]}"
    do
      export "$variable"
    done
    

提交回复
热议问题