2

In a bash script, if I have a variable $FOO which equals "BAR", how would I set the environment variable named BAR equal to "BAZ"? Thanks.

3
  • 1
    Maybe export BAR="BAZ"? Commented May 15, 2018 at 22:11
  • Evidently it's simply $FOO=BAZ but it wasn't working for me because my variable was an array entry. I'll get the array entry value into a scalar and then I should be good. Thanks. Commented May 15, 2018 at 22:16
  • with simpy $FOO=BAZ got: BAR=BAZ: command not found. ` export $FOO=BAZ ` did the job, though it also exports. Commented Sep 17, 2020 at 18:10

1 Answer 1

2

export $(echo "$(echo $FOO)=BAZ") should work. I'm using bash --version = 4.1.2 with success.

export FOO=BAR
export $(echo "$(echo $FOO)=BAZ")
echo $BAR

BAZ

Sign up to request clarification or add additional context in comments.

1 Comment

just ` export $FOO=BAZ ` did the job too,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.