Can I export a variable to the environment from a bash script without sourcing it?

前端 未结 7 1798
半阙折子戏
半阙折子戏 2020-11-22 05:51

Suppose that I have this script

export.bash:

#! /usr/bin/env bash
export VAR=\"HELLO, VARIABLE\"

When I execute th

7条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 06:32

    Found an interesting and neat way to export environment variables from a file:

    in env.vars:

    foo=test
    

    test script:

    eval `cat env.vars`
    echo $foo         # => test
    sh -c 'echo $foo' # => 
    
    export eval `cat env.vars`
    echo $foo         # => test
    sh -c 'echo $foo' # => test
    
    # a better one
    export `cat env.vars`
    echo $foo         # => test
    sh -c 'echo $foo' # => test
    

提交回复
热议问题