0

I set a variable that contains multiple values. (this is what I get from heroku config --shell, redacted)
The command to save command output was

envvars=`heroku config --shell -a heroku-app`

And echo "$envvars" result is like this:

BH_SERVER=000000000000000000
DATABASE_URL='postgres://stringno1:[email protected]:5432/randomstring2'
DISCORD_TOKEN=some.veryveryveryvery.long.token.string
EXM_SERVER=000000000000000001
MUTED_SERVER='000000000000000002, 000000000000000003'
SUPER_USER='000000000000000004, 000000000000000005'
TEST_SERVER=000000000000000006
TZ=Asia/Seoul

Now I want to set these values to environment variables. These must not be permanant because these are only needed by python app which will be executed later in script. I think I can use = as delimiter for distinguish key and value but not 100% sure about that. (I don't know how heroku convar works well)

I expect these-like output.

echo $BH_SERVER
# 000000000000000000
echo $DATABASE_URL
# postgres://stringno1:[email protected]:5432/randomstring2
# Note that there is no ' in start and end of line
echo $DISCORD_TOKEN
# some.veryveryveryvery.long.token.string
# Note that . is still there.
echo $MUTED_SERVER
# 000000000000000002, 000000000000000003
# Even if there is space in string, it should be treated as one line
# Also, there is no ' in start and end of line
echo $TZ
# Asia/Seoul
# / is not interpreted. I think this is normal.

There are some already-answered question but I don't find any answer that match my condition.

I put git-bash tag because I am working on Git Bash but I think solution of this question would be same as Bash.

2
  • did you try eval $envvars? Commented Dec 9, 2020 at 11:24
  • @Lety you mean eval $envvar? that just stops script working. I think it is waiting user input but not sure. Commented Dec 9, 2020 at 11:41

3 Answers 3

2

You can use eval. Make sure to use the double quotes or else the settings with spaces and quotes will get mangled.

eval "$envvars"

or

eval "$(heroku config --shell -a heroku-app)"

You can use a subshell to isolate the changes to a section of code.

# Parentheses create a subshell, or child process.
# Variables set in a child process don't affect the parent.
(
    eval "$(heroku config --shell -a heroku-app)"
    echo "$BH_SERVER"
    echo "$DATABASE_URL"
    echo "$DISCORD_TOKEN"
    echo "$MUTED_SERVER"
    echo "$TZ"
)

# The variables will be unset here.
Sign up to request clarification or add additional context in comments.

Comments

1

You can add echo output to temporary file, source the file and then delete the temp file :

echo $envvars > /tmp/tmp.env && source /tmp/tmp.env && rm /tmp/tmp.env

If on gitbash /tmp is not available, use another available path to create the temporary file

Alternately you can use below format which will not need temporary file

echo $envvars | source /dev/stdin

4 Comments

surprisingly, it works! I don't like concept of 'creating new temporary file' but it works. I wonder is there any way to not create 'temporary' files though... Thanks anyway.
I have modified the answer, Can you try the second format without the temp file "echo $envvars | source /dev/stdin"
The second one won't work. Pipelines run in subshells.
second method doesn't work. git-bash seems don't like /dev/stdin stuff. Anyway, it seems git-bash seems using Windows' own temporary directory(%temp%) which is nice to know.
0

I'm not sure but this should work

eval $(echo $envvars) ./script.sh

or

eval $(echo $envvars) python script.py

2 Comments

both doesn't work neither. it says 'not a valid identifier' when processing '0000, 0000'-like lines
I've updated the anwer to address this case

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.