1

I need some help with my script.

I have environmental variables like these:

SECRET_OF_AN_APP_dev=123
SECRET_OF_AN_APP_test=456

SECRET_OF_AN_APP_prod=789

I want to have a script that will change SECRET_OF_AN_APP_dev=123 to SECRET_OF_AN_APP=123 and export it into env in first iteration, then SECRET_OF_AN_APP_test=456 change to SECRET_OF_AN_APP=456 in second iteration etc.

#!/bin/bash
APP_SECRET=SECRET_OF_AN_APP
ENVIRONMENT_NAME=dev
LIST_OF_ENVIRONMENTS=[dev, prod, test...]

for i in $LIST_OF_ENVIRONMENTS
do
APP_SECRET=$(env | grep APP_SECRET_$i | sed 's/APP_SECRET_'"$i"'=//g' ) >> var.bash


. ./var.bash

./some_script_where_$APP_SECRET_is_required.sh

done

I think my script is working in something like a subshell because when I do: echo $SECRET_OF_AN_APP, it's blank.

I need to have it set because other scripts which are executing in this loop need those variables.

I tried with export, with ./var.bash, with set -o allexport, etc.

Nothing is working.

I need to have "echo $SECRET_OF_AN_APP" in my shell, not only script. It's working in script. but it's not set in my environmental variables.

1
  • 1
    You seem to mix SECRET_OF_AN_APP and APP_SECRET. Please edit your question and make sure all parts are consistent. Copy&paste the exact code you run on your system. Use shellcheck.net. Apart from all other problems and inconsistencies, you have to export the variable to make it available for the called script (unless you instructed the shell to export all variables).
    – Bodo
    Commented Apr 7, 2022 at 12:21

2 Answers 2

1

One piece that is tripping you up is that your env | grep APP_SECRET_$i is not substituting the value of APP_SECRET to the expected variable name of SECRET_OF_AN_APP. Add a variable expansion to APP_SECRET to fix that.

You've captured the output of $(env ...) into an assignment APP_SECRET and then redirected the output of that assignment to the file. The output from the command substitution may be correct, and assigned to APP_SECRET, but the text of that assignment is not present in the output file.

Either export APP_SECRET (since it's been assigned in this shell script), or print the assignment into the file before you source it and skip the redirection on the assignment.

Either:

export APP_SECRET

or:

APP_SECRET=$(varname=${APP_SECRET}_${i}; printf '%s' "${!varname}" )
declare -p APP_SECRET >> var.bash
echo "export APP_SECRET" >> var.bash # if you need it to be exported 

I've simplified your command substitution so that it puts together a variable that holds the name of the actual variable value that you want. Be careful to use braces here so that the underscore isn't seen as part of the APP_SECRET variable. Then we use bash's variable indirection expansion to expand "varname" into the name of the variable and then take the value of that variable. This translates "varname" into "SECRET_OF_AN_APP_dev", for example, and then looks for the value of that (e.g. 123).

If the value of APP_SECRET needs to be available to your shell after this script exits, then your parent shell needs to source in the var.bash file:

. ./var.bash

See also:

5
  • env | grep APP_SECRET_$i << this is ok. "APP_SECRET" in this case isn't variable. I tried your code but It also looks ok in script, but I can't see that APP_SECRET=some.value in my environmental variables.
    – guesto
    Commented Apr 7, 2022 at 13:25
  • Did you leave the . ./var.bash statement? That should source in the contents of that file, which should contain APP_SECRET=123 (or other value) and the line export APP_SECRET. Can you check those things?
    – Jeff Schaller
    Commented Apr 7, 2022 at 13:36
  • yes is all working. I mean file with export APP_SECRET=123 and echo $APP_SECRET in script is working, but no in the shell. $APP_SECRET is not in my environmental variables after script is done.
    – guesto
    Commented Apr 7, 2022 at 13:52
  • Then I've misunderstood your question. You want APP_SECRET to be in the parent environment of this script, so that it's available after this script exits?
    – Jeff Schaller
    Commented Apr 7, 2022 at 13:53
  • Yes I want that way, I think It could be the solution in my spaghetti scripts :)
    – guesto
    Commented Apr 7, 2022 at 13:57
0

Is this what are you looking for?

#!/bin/bash
SECRET_OF_AN_APP_dev=123
SECRET_OF_AN_APP_test=456
SECRET_OF_AN_APP_prod=789

for s in dev test prod
do
   name=SECRET_OF_AN_APP_$s
   echo $name
   SECRET_OF_AN_APP=${!name}
   echo $SECRET_OF_AN_APP
   export SECRET_OF_AN_APP
   some_app_which_uses_that_variable
done
3
  • yes, but I need to have "echo $SECRET_OF_AN_APP" in my shell, not only script. It's working in script. but it's not set in my environmental variables.
    – guesto
    Commented Apr 7, 2022 at 12:17
  • @guesto please edit your question and clarify that.
    – terdon
    Commented Apr 7, 2022 at 12:50
  • @terdon edited.
    – guesto
    Commented Apr 7, 2022 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .