0

Let's say I have script alpha.sh. The overly simplified version of it is as follows:

#!/usr/bin/bash

ENV_PARAMS_FOR_BRAVO="FOO=bar"

result=$($ENV_PARAMS_FOR_BRAVO bash bravo.sh)
echo $results

And then a script bravo.sh, whose overly simplified version is:

echo "foo is $FOO"

When I run the above alpha script, I get an error, because it tries to execute $ENV_PARAMS_FOR_BRAVO as if it was the command itself. When I run the same exact command replacing the above variable with its contents (result=$(FOO=bar bash bravo.sh)), the script works as expected.

In the actual version of the script, I need to pass a lot more variables to bravo from alpha, so the $ENV_PARAMS_FOR_BRAVO variable is meant to serve as an aggregation for a curated list of arguments for bravo. I also can't pass them to bravo as arguments because many of these are optional and will shift the real arguments bravo takes that are NOT optional.

What's the issue with the above approach and how do I work around it?

1 Answer 1

1

You can export the variable FOO so it is visible for all calling scripts:

#!/usr/bin/bash

export FOO="bar"

result=$(./bravo.sh)
echo $result

> foo is bar

Alternate solution if you only want it to bravo.sh and not to all following scripts:

#!/usr/bin/bash
result=$(export FOO="bar"; ./bravo.sh)
echo $result

result2=$(./bravo.sh)
echo $result2

Output will be

> foo is bar
> foo is
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.