I need some help with my script.

I have environmental variables like these:



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.

LIST_OF_ENVIRONMENTS=[dev, prod, test...]

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

. ./var.bash



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
    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
    Apr 7, 2022 at 12:21

2 Answers 2


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.




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:

  • 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
    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
    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
    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
    Apr 7, 2022 at 13:53
  • Yes I want that way, I think It could be the solution in my spaghetti scripts :)
    – guesto
    Apr 7, 2022 at 13:57

Is this what are you looking for?


for s in dev test prod
   echo $name
   export SECRET_OF_AN_APP
  • 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
    Apr 7, 2022 at 12:17
  • @guesto please edit your question and clarify that.
    – terdon
    Apr 7, 2022 at 12:50
  • @terdon edited.
    – guesto
    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 .