I want to unset a list of bash variables that have their variable strings stored in an array Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionHow do I set an environment variable for sudo in MacOS?How to unset range of array in Bashssh-agent not getting set up (SSH_AUTH_SOCK, SSH_AGENT_PID env vars not set)Is it a good idea to put environment variables to /usr/local/binBash convert string to array of strings?The environment given to a program vs the execution environment in which the program is invokedHow do I store bash env vars in array then access/modify?Referencing bash array variables from another arrayBash Script - How can I concatenate several strings containing special characters?bash defined array and compare input to stored array value

What are the pros and cons of Aerospike nosecones?

What causes the vertical darker bands in my photo?

What is the musical term for a note that continously plays through a melody?

Why is black pepper both grey and black?

Is there a concise way to say "all of the X, one of each"?

What does the "x" in "x86" represent?

How to bypass password on Windows XP account?

Right-skewed distribution with mean equals to mode?

Sorting numerically

Is the address of a local variable a constexpr?

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Gastric acid as a weapon

If Jon Snow became King of the Seven Kingdoms what would his regnal number be?

Why was the term "discrete" used in discrete logarithm?

Should I discuss the type of campaign with my players?

If a contract sometimes uses the wrong name, is it still valid?

What is a Meta algorithm?

Why is "Captain Marvel" translated as male in Portugal?

When is phishing education going too far?

What LEGO pieces have "real-world" functionality?

What's the purpose of writing one's academic bio in 3rd person?

How do I stop a creek from eroding my steep embankment?

Single word antonym of "flightless"

How to do this path/lattice with tikz



I want to unset a list of bash variables that have their variable strings stored in an array



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionHow do I set an environment variable for sudo in MacOS?How to unset range of array in Bashssh-agent not getting set up (SSH_AUTH_SOCK, SSH_AGENT_PID env vars not set)Is it a good idea to put environment variables to /usr/local/binBash convert string to array of strings?The environment given to a program vs the execution environment in which the program is invokedHow do I store bash env vars in array then access/modify?Referencing bash array variables from another arrayBash Script - How can I concatenate several strings containing special characters?bash defined array and compare input to stored array value



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm working through creating a Dockerfile that calls on a script named start.sh I want to take the environment variables that are passed from the user either by means of docker-compose.yml or docker run -e now what I've wanted to do is something where I have a list of these variables defined dynamically in an array.



## Environment Variables
## Missing Vars off the hop SLACK_HOOK_URL
CONFIG_SETTINGS=(
AWS_ID
AWS_SECRET
BUCKET_REGION
BUCKET_NAME
DB_USER
DB_PASSWORD
DB_HOSTIP
DB_PORT
DB_NAME
)


I only have to unset a subset of the above variables defined in this array.



CONFIG_SECRETS=(
AWS_ID
AWS_ID_STR
AWS_SECRET
AWS_SECRET_STR
BUCKET_NAME
BUCKET_NAME_STR
DB_USER
DB_USER_STR
DB_PASSWORD
DB_PASSWORD_STR
DB_HOSTIP
DB_HOSTIP_STR
DB_PORT
DB_PORT_STR
DB_NAME
DB_NAME_STR
)


My problem is the following for loop.



## Sanitization section
for i in "$!CONFIG_SECRETS[@]"; do
## Nullify environment variables that contain secrets before launching.
# echo $CONFIG_SECRETS[$i]
eval $"$CONFIG_SECRETS[$i]"=$""
unset $"$CONFIG_SECRETS[$i]"
echo $$CONFIG_SECRETS[$i] is now to: $"$CONFIG_SECRETS[$i]"
done


For example, the below is a for loop I created to iterate through these variables and set them.



for i in "$!CONFIG_SETTINGS[@]"; do
echo $CONFIG_SETTINGS[$i]"_KEY"
## Indirect references http://tldp.org/LDP/abs/html/ivr.html
eval FROM_STRING=$"$CONFIG_SETTINGS[$i]_STR"
eval VALUE_STRING=$$CONFIG_SETTINGS[$i]
eval KEY_STRING=$"$CONFIG_SETTINGS[$i]_KEY"
TO_STRING="$KEY_STRING$VALUE_STRING"
sed -i '' "s/$FROM_STRING/$TO_STRING/g" ./config.tmpl
done


This modifies the following configuration file.




"aws_id": "YOUR-AWS-ID",
"aws_secret": "YOUR-AWS-SECRET",
"bucket_region": "YOUR-BUCKET-REGION",
"bucket_name": "YOUR-BUCKET-NAME",
"db_conn": "USER:PASSWORD@tcp(localhost:3306)/DBNAME",
"slack_hook_url": ""



The full context script minus things that are outside of the scope of this question.



#!/bin/bash

## Launch service will tell prism-bin what mode to run in.
LAUNCHMODE="$MODE:-$1"

## This variable will be what can override default launch args. I may modify this as I learn more about prism-bin
LAUNCHARGS="$CUSTOM_ARGS:-$2"

## This is setup this way to handle any situations that might arise from the
## config being JSON and bash not being any good at JSON.
# ## Strings to replace.
AWS_ID_STR="YOUR-AWS-ID"
AWS_SECRET_STR="YOUR-AWS-SECRET"
BUCKET_REGION_STR="YOUR-BUCKET-REGION"
BUCKET_NAME_STR="YOUR-BUCKET-NAME"
DB_USER_STR="USER"
DB_PASSWORD_STR="PASSWORD"
DB_HOSTIP_STR="localhost"
DB_PORT_STR="3306"
DB_NAME_STR="DBNAME"

# Environment Variables/Defaults
## Json sucks in BASH/Shell so you need to add trailing commas intermittently.
## Just pay attention to this. Also at some point I'll need to make a fringe
## case for handling key/values that aren't included in the default config.
AWS_ID="$AWS_ID:-potato"
AWS_SECRET="$AWS_SECRET:-potato"
BUCKET_REGION="$BUCKET_REGION:-potato"
BUCKET_NAME="$BUCKET_NAME:-potato"
DB_USER="$DB_USER:-potato"
DB_PASSWORD="$DB_PASSWORD:-potato"
DB_HOSTIP="$DB_HOSTIP:-potato"
DB_PORT="$DB_PORT:-potato"
DB_NAME="$DB_NAME:-potato"

## Environment Variables in Array
CONFIG_SETTINGS=(
AWS_ID
AWS_SECRET
BUCKET_REGION
BUCKET_NAME
DB_USER
DB_PASSWORD
DB_HOSTIP
DB_PORT
DB_NAME
)
CONFIG_SECRETS=(
AWS_ID
AWS_ID_STR
AWS_SECRET
AWS_SECRET_STR
BUCKET_NAME
BUCKET_NAME_STR
DB_USER
DB_USER_STR
DB_PASSWORD
DB_PASSWORD_STR
DB_HOSTIP
DB_HOSTIP_STR
DB_PORT
DB_PORT_STR
DB_NAME
DB_NAME_STR
)

## Sanitization section
# Awaiting someone smarter than me to suggest a method for this.
# https://unix.stackexchange.com/questions/474097/i-want-to-unset-a-list-of-bash-variables-that-have-their-variable-strings-stored
for i in "$CONFIG_SECRETS[@]"; do
unset $i
eval echo $"$CONFIG_SECRETS[$i]"=$$CONFIG_SETTINGS[$i]
done









share|improve this question






























    0















    I'm working through creating a Dockerfile that calls on a script named start.sh I want to take the environment variables that are passed from the user either by means of docker-compose.yml or docker run -e now what I've wanted to do is something where I have a list of these variables defined dynamically in an array.



    ## Environment Variables
    ## Missing Vars off the hop SLACK_HOOK_URL
    CONFIG_SETTINGS=(
    AWS_ID
    AWS_SECRET
    BUCKET_REGION
    BUCKET_NAME
    DB_USER
    DB_PASSWORD
    DB_HOSTIP
    DB_PORT
    DB_NAME
    )


    I only have to unset a subset of the above variables defined in this array.



    CONFIG_SECRETS=(
    AWS_ID
    AWS_ID_STR
    AWS_SECRET
    AWS_SECRET_STR
    BUCKET_NAME
    BUCKET_NAME_STR
    DB_USER
    DB_USER_STR
    DB_PASSWORD
    DB_PASSWORD_STR
    DB_HOSTIP
    DB_HOSTIP_STR
    DB_PORT
    DB_PORT_STR
    DB_NAME
    DB_NAME_STR
    )


    My problem is the following for loop.



    ## Sanitization section
    for i in "$!CONFIG_SECRETS[@]"; do
    ## Nullify environment variables that contain secrets before launching.
    # echo $CONFIG_SECRETS[$i]
    eval $"$CONFIG_SECRETS[$i]"=$""
    unset $"$CONFIG_SECRETS[$i]"
    echo $$CONFIG_SECRETS[$i] is now to: $"$CONFIG_SECRETS[$i]"
    done


    For example, the below is a for loop I created to iterate through these variables and set them.



    for i in "$!CONFIG_SETTINGS[@]"; do
    echo $CONFIG_SETTINGS[$i]"_KEY"
    ## Indirect references http://tldp.org/LDP/abs/html/ivr.html
    eval FROM_STRING=$"$CONFIG_SETTINGS[$i]_STR"
    eval VALUE_STRING=$$CONFIG_SETTINGS[$i]
    eval KEY_STRING=$"$CONFIG_SETTINGS[$i]_KEY"
    TO_STRING="$KEY_STRING$VALUE_STRING"
    sed -i '' "s/$FROM_STRING/$TO_STRING/g" ./config.tmpl
    done


    This modifies the following configuration file.




    "aws_id": "YOUR-AWS-ID",
    "aws_secret": "YOUR-AWS-SECRET",
    "bucket_region": "YOUR-BUCKET-REGION",
    "bucket_name": "YOUR-BUCKET-NAME",
    "db_conn": "USER:PASSWORD@tcp(localhost:3306)/DBNAME",
    "slack_hook_url": ""



    The full context script minus things that are outside of the scope of this question.



    #!/bin/bash

    ## Launch service will tell prism-bin what mode to run in.
    LAUNCHMODE="$MODE:-$1"

    ## This variable will be what can override default launch args. I may modify this as I learn more about prism-bin
    LAUNCHARGS="$CUSTOM_ARGS:-$2"

    ## This is setup this way to handle any situations that might arise from the
    ## config being JSON and bash not being any good at JSON.
    # ## Strings to replace.
    AWS_ID_STR="YOUR-AWS-ID"
    AWS_SECRET_STR="YOUR-AWS-SECRET"
    BUCKET_REGION_STR="YOUR-BUCKET-REGION"
    BUCKET_NAME_STR="YOUR-BUCKET-NAME"
    DB_USER_STR="USER"
    DB_PASSWORD_STR="PASSWORD"
    DB_HOSTIP_STR="localhost"
    DB_PORT_STR="3306"
    DB_NAME_STR="DBNAME"

    # Environment Variables/Defaults
    ## Json sucks in BASH/Shell so you need to add trailing commas intermittently.
    ## Just pay attention to this. Also at some point I'll need to make a fringe
    ## case for handling key/values that aren't included in the default config.
    AWS_ID="$AWS_ID:-potato"
    AWS_SECRET="$AWS_SECRET:-potato"
    BUCKET_REGION="$BUCKET_REGION:-potato"
    BUCKET_NAME="$BUCKET_NAME:-potato"
    DB_USER="$DB_USER:-potato"
    DB_PASSWORD="$DB_PASSWORD:-potato"
    DB_HOSTIP="$DB_HOSTIP:-potato"
    DB_PORT="$DB_PORT:-potato"
    DB_NAME="$DB_NAME:-potato"

    ## Environment Variables in Array
    CONFIG_SETTINGS=(
    AWS_ID
    AWS_SECRET
    BUCKET_REGION
    BUCKET_NAME
    DB_USER
    DB_PASSWORD
    DB_HOSTIP
    DB_PORT
    DB_NAME
    )
    CONFIG_SECRETS=(
    AWS_ID
    AWS_ID_STR
    AWS_SECRET
    AWS_SECRET_STR
    BUCKET_NAME
    BUCKET_NAME_STR
    DB_USER
    DB_USER_STR
    DB_PASSWORD
    DB_PASSWORD_STR
    DB_HOSTIP
    DB_HOSTIP_STR
    DB_PORT
    DB_PORT_STR
    DB_NAME
    DB_NAME_STR
    )

    ## Sanitization section
    # Awaiting someone smarter than me to suggest a method for this.
    # https://unix.stackexchange.com/questions/474097/i-want-to-unset-a-list-of-bash-variables-that-have-their-variable-strings-stored
    for i in "$CONFIG_SECRETS[@]"; do
    unset $i
    eval echo $"$CONFIG_SECRETS[$i]"=$$CONFIG_SETTINGS[$i]
    done









    share|improve this question


























      0












      0








      0


      0






      I'm working through creating a Dockerfile that calls on a script named start.sh I want to take the environment variables that are passed from the user either by means of docker-compose.yml or docker run -e now what I've wanted to do is something where I have a list of these variables defined dynamically in an array.



      ## Environment Variables
      ## Missing Vars off the hop SLACK_HOOK_URL
      CONFIG_SETTINGS=(
      AWS_ID
      AWS_SECRET
      BUCKET_REGION
      BUCKET_NAME
      DB_USER
      DB_PASSWORD
      DB_HOSTIP
      DB_PORT
      DB_NAME
      )


      I only have to unset a subset of the above variables defined in this array.



      CONFIG_SECRETS=(
      AWS_ID
      AWS_ID_STR
      AWS_SECRET
      AWS_SECRET_STR
      BUCKET_NAME
      BUCKET_NAME_STR
      DB_USER
      DB_USER_STR
      DB_PASSWORD
      DB_PASSWORD_STR
      DB_HOSTIP
      DB_HOSTIP_STR
      DB_PORT
      DB_PORT_STR
      DB_NAME
      DB_NAME_STR
      )


      My problem is the following for loop.



      ## Sanitization section
      for i in "$!CONFIG_SECRETS[@]"; do
      ## Nullify environment variables that contain secrets before launching.
      # echo $CONFIG_SECRETS[$i]
      eval $"$CONFIG_SECRETS[$i]"=$""
      unset $"$CONFIG_SECRETS[$i]"
      echo $$CONFIG_SECRETS[$i] is now to: $"$CONFIG_SECRETS[$i]"
      done


      For example, the below is a for loop I created to iterate through these variables and set them.



      for i in "$!CONFIG_SETTINGS[@]"; do
      echo $CONFIG_SETTINGS[$i]"_KEY"
      ## Indirect references http://tldp.org/LDP/abs/html/ivr.html
      eval FROM_STRING=$"$CONFIG_SETTINGS[$i]_STR"
      eval VALUE_STRING=$$CONFIG_SETTINGS[$i]
      eval KEY_STRING=$"$CONFIG_SETTINGS[$i]_KEY"
      TO_STRING="$KEY_STRING$VALUE_STRING"
      sed -i '' "s/$FROM_STRING/$TO_STRING/g" ./config.tmpl
      done


      This modifies the following configuration file.




      "aws_id": "YOUR-AWS-ID",
      "aws_secret": "YOUR-AWS-SECRET",
      "bucket_region": "YOUR-BUCKET-REGION",
      "bucket_name": "YOUR-BUCKET-NAME",
      "db_conn": "USER:PASSWORD@tcp(localhost:3306)/DBNAME",
      "slack_hook_url": ""



      The full context script minus things that are outside of the scope of this question.



      #!/bin/bash

      ## Launch service will tell prism-bin what mode to run in.
      LAUNCHMODE="$MODE:-$1"

      ## This variable will be what can override default launch args. I may modify this as I learn more about prism-bin
      LAUNCHARGS="$CUSTOM_ARGS:-$2"

      ## This is setup this way to handle any situations that might arise from the
      ## config being JSON and bash not being any good at JSON.
      # ## Strings to replace.
      AWS_ID_STR="YOUR-AWS-ID"
      AWS_SECRET_STR="YOUR-AWS-SECRET"
      BUCKET_REGION_STR="YOUR-BUCKET-REGION"
      BUCKET_NAME_STR="YOUR-BUCKET-NAME"
      DB_USER_STR="USER"
      DB_PASSWORD_STR="PASSWORD"
      DB_HOSTIP_STR="localhost"
      DB_PORT_STR="3306"
      DB_NAME_STR="DBNAME"

      # Environment Variables/Defaults
      ## Json sucks in BASH/Shell so you need to add trailing commas intermittently.
      ## Just pay attention to this. Also at some point I'll need to make a fringe
      ## case for handling key/values that aren't included in the default config.
      AWS_ID="$AWS_ID:-potato"
      AWS_SECRET="$AWS_SECRET:-potato"
      BUCKET_REGION="$BUCKET_REGION:-potato"
      BUCKET_NAME="$BUCKET_NAME:-potato"
      DB_USER="$DB_USER:-potato"
      DB_PASSWORD="$DB_PASSWORD:-potato"
      DB_HOSTIP="$DB_HOSTIP:-potato"
      DB_PORT="$DB_PORT:-potato"
      DB_NAME="$DB_NAME:-potato"

      ## Environment Variables in Array
      CONFIG_SETTINGS=(
      AWS_ID
      AWS_SECRET
      BUCKET_REGION
      BUCKET_NAME
      DB_USER
      DB_PASSWORD
      DB_HOSTIP
      DB_PORT
      DB_NAME
      )
      CONFIG_SECRETS=(
      AWS_ID
      AWS_ID_STR
      AWS_SECRET
      AWS_SECRET_STR
      BUCKET_NAME
      BUCKET_NAME_STR
      DB_USER
      DB_USER_STR
      DB_PASSWORD
      DB_PASSWORD_STR
      DB_HOSTIP
      DB_HOSTIP_STR
      DB_PORT
      DB_PORT_STR
      DB_NAME
      DB_NAME_STR
      )

      ## Sanitization section
      # Awaiting someone smarter than me to suggest a method for this.
      # https://unix.stackexchange.com/questions/474097/i-want-to-unset-a-list-of-bash-variables-that-have-their-variable-strings-stored
      for i in "$CONFIG_SECRETS[@]"; do
      unset $i
      eval echo $"$CONFIG_SECRETS[$i]"=$$CONFIG_SETTINGS[$i]
      done









      share|improve this question
















      I'm working through creating a Dockerfile that calls on a script named start.sh I want to take the environment variables that are passed from the user either by means of docker-compose.yml or docker run -e now what I've wanted to do is something where I have a list of these variables defined dynamically in an array.



      ## Environment Variables
      ## Missing Vars off the hop SLACK_HOOK_URL
      CONFIG_SETTINGS=(
      AWS_ID
      AWS_SECRET
      BUCKET_REGION
      BUCKET_NAME
      DB_USER
      DB_PASSWORD
      DB_HOSTIP
      DB_PORT
      DB_NAME
      )


      I only have to unset a subset of the above variables defined in this array.



      CONFIG_SECRETS=(
      AWS_ID
      AWS_ID_STR
      AWS_SECRET
      AWS_SECRET_STR
      BUCKET_NAME
      BUCKET_NAME_STR
      DB_USER
      DB_USER_STR
      DB_PASSWORD
      DB_PASSWORD_STR
      DB_HOSTIP
      DB_HOSTIP_STR
      DB_PORT
      DB_PORT_STR
      DB_NAME
      DB_NAME_STR
      )


      My problem is the following for loop.



      ## Sanitization section
      for i in "$!CONFIG_SECRETS[@]"; do
      ## Nullify environment variables that contain secrets before launching.
      # echo $CONFIG_SECRETS[$i]
      eval $"$CONFIG_SECRETS[$i]"=$""
      unset $"$CONFIG_SECRETS[$i]"
      echo $$CONFIG_SECRETS[$i] is now to: $"$CONFIG_SECRETS[$i]"
      done


      For example, the below is a for loop I created to iterate through these variables and set them.



      for i in "$!CONFIG_SETTINGS[@]"; do
      echo $CONFIG_SETTINGS[$i]"_KEY"
      ## Indirect references http://tldp.org/LDP/abs/html/ivr.html
      eval FROM_STRING=$"$CONFIG_SETTINGS[$i]_STR"
      eval VALUE_STRING=$$CONFIG_SETTINGS[$i]
      eval KEY_STRING=$"$CONFIG_SETTINGS[$i]_KEY"
      TO_STRING="$KEY_STRING$VALUE_STRING"
      sed -i '' "s/$FROM_STRING/$TO_STRING/g" ./config.tmpl
      done


      This modifies the following configuration file.




      "aws_id": "YOUR-AWS-ID",
      "aws_secret": "YOUR-AWS-SECRET",
      "bucket_region": "YOUR-BUCKET-REGION",
      "bucket_name": "YOUR-BUCKET-NAME",
      "db_conn": "USER:PASSWORD@tcp(localhost:3306)/DBNAME",
      "slack_hook_url": ""



      The full context script minus things that are outside of the scope of this question.



      #!/bin/bash

      ## Launch service will tell prism-bin what mode to run in.
      LAUNCHMODE="$MODE:-$1"

      ## This variable will be what can override default launch args. I may modify this as I learn more about prism-bin
      LAUNCHARGS="$CUSTOM_ARGS:-$2"

      ## This is setup this way to handle any situations that might arise from the
      ## config being JSON and bash not being any good at JSON.
      # ## Strings to replace.
      AWS_ID_STR="YOUR-AWS-ID"
      AWS_SECRET_STR="YOUR-AWS-SECRET"
      BUCKET_REGION_STR="YOUR-BUCKET-REGION"
      BUCKET_NAME_STR="YOUR-BUCKET-NAME"
      DB_USER_STR="USER"
      DB_PASSWORD_STR="PASSWORD"
      DB_HOSTIP_STR="localhost"
      DB_PORT_STR="3306"
      DB_NAME_STR="DBNAME"

      # Environment Variables/Defaults
      ## Json sucks in BASH/Shell so you need to add trailing commas intermittently.
      ## Just pay attention to this. Also at some point I'll need to make a fringe
      ## case for handling key/values that aren't included in the default config.
      AWS_ID="$AWS_ID:-potato"
      AWS_SECRET="$AWS_SECRET:-potato"
      BUCKET_REGION="$BUCKET_REGION:-potato"
      BUCKET_NAME="$BUCKET_NAME:-potato"
      DB_USER="$DB_USER:-potato"
      DB_PASSWORD="$DB_PASSWORD:-potato"
      DB_HOSTIP="$DB_HOSTIP:-potato"
      DB_PORT="$DB_PORT:-potato"
      DB_NAME="$DB_NAME:-potato"

      ## Environment Variables in Array
      CONFIG_SETTINGS=(
      AWS_ID
      AWS_SECRET
      BUCKET_REGION
      BUCKET_NAME
      DB_USER
      DB_PASSWORD
      DB_HOSTIP
      DB_PORT
      DB_NAME
      )
      CONFIG_SECRETS=(
      AWS_ID
      AWS_ID_STR
      AWS_SECRET
      AWS_SECRET_STR
      BUCKET_NAME
      BUCKET_NAME_STR
      DB_USER
      DB_USER_STR
      DB_PASSWORD
      DB_PASSWORD_STR
      DB_HOSTIP
      DB_HOSTIP_STR
      DB_PORT
      DB_PORT_STR
      DB_NAME
      DB_NAME_STR
      )

      ## Sanitization section
      # Awaiting someone smarter than me to suggest a method for this.
      # https://unix.stackexchange.com/questions/474097/i-want-to-unset-a-list-of-bash-variables-that-have-their-variable-strings-stored
      for i in "$CONFIG_SECRETS[@]"; do
      unset $i
      eval echo $"$CONFIG_SECRETS[$i]"=$$CONFIG_SETTINGS[$i]
      done






      bash environment-variables docker






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 hours ago









      Rui F Ribeiro

      42.1k1483142




      42.1k1483142










      asked Oct 8 '18 at 23:48









      chamunkschamunks

      33




      33




















          3 Answers
          3






          active

          oldest

          votes


















          1














          This should be a simple loop



          #!/bin/bash

          UNSET=(a b c)

          a=10 b=20 c=30 d=40 e=50

          echo Before a=$a b=$b c=$c d=$d e=$e

          for i in $UNSET[@]
          do
          unset $i
          done

          echo After a=$a b=$b c=$c d=$d e=$e


          This results in



          Before a=10 b=20 c=30 d=40 e=50
          After a= b= c= d=40 e=50





          share|improve this answer

























          • Looks good, works well. Tyvm

            – chamunks
            Oct 9 '18 at 1:01


















          0














          You can dereference variables with !:



          $ foo=bar
          $ bar=1
          $ echo $!foo
          1


          You can't use a variable inside an expansion like that though, so you will probably have to do something really ugly with eval.






          share|improve this answer


















          • 1





            Hmm so, I'm not sure how this answers the question precisely. I'm looking to use the strings in the arrays as variable names. Which in the for loop I'm trying to dereference. Pardon my ignorance here I'm just a bit confused as to how to apply it. Also, I appreciate the response and gave an upvote but apparently I'm too new for it to count. Sorry </3

            – chamunks
            Oct 9 '18 at 0:11



















          0














          I think you've worked yourself into a circle; simply loop over the array and unset the parameter referred to by the loop:



          AWS_SECRET='secret_here'
          echo Before:
          declare -p AWS_SECRET
          set|grep AWS_SECRET

          for secret in "$CONFIG_SECRETS[@]"
          do
          unset $secret
          done

          echo After:
          declare -p AWS_SECRET
          set|grep AWS_SECRET





          share|improve this answer























          • I feel like I've worked myself into a circle at this point :P Although I wonder if by your suggestion I would just be unsetting $secret and not the environment variable represented by $secret

            – chamunks
            Oct 9 '18 at 0:39











          • That's why I set up a sample variable (AWS_SECRET), to confirm that it's set before the loop and not set after the loop -- test it and see!

            – Jeff Schaller
            Oct 9 '18 at 0:40











          • Also, keep in mind that the variable name is defined as a string within in an array and not your normal way so the example should contain a dynamically referenced string if possible. as its the problem that I'm having, unsetting parameters normally is much easier than doing what I'm doing.

            – chamunks
            Oct 9 '18 at 0:45











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474097%2fi-want-to-unset-a-list-of-bash-variables-that-have-their-variable-strings-stored%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This should be a simple loop



          #!/bin/bash

          UNSET=(a b c)

          a=10 b=20 c=30 d=40 e=50

          echo Before a=$a b=$b c=$c d=$d e=$e

          for i in $UNSET[@]
          do
          unset $i
          done

          echo After a=$a b=$b c=$c d=$d e=$e


          This results in



          Before a=10 b=20 c=30 d=40 e=50
          After a= b= c= d=40 e=50





          share|improve this answer

























          • Looks good, works well. Tyvm

            – chamunks
            Oct 9 '18 at 1:01















          1














          This should be a simple loop



          #!/bin/bash

          UNSET=(a b c)

          a=10 b=20 c=30 d=40 e=50

          echo Before a=$a b=$b c=$c d=$d e=$e

          for i in $UNSET[@]
          do
          unset $i
          done

          echo After a=$a b=$b c=$c d=$d e=$e


          This results in



          Before a=10 b=20 c=30 d=40 e=50
          After a= b= c= d=40 e=50





          share|improve this answer

























          • Looks good, works well. Tyvm

            – chamunks
            Oct 9 '18 at 1:01













          1












          1








          1







          This should be a simple loop



          #!/bin/bash

          UNSET=(a b c)

          a=10 b=20 c=30 d=40 e=50

          echo Before a=$a b=$b c=$c d=$d e=$e

          for i in $UNSET[@]
          do
          unset $i
          done

          echo After a=$a b=$b c=$c d=$d e=$e


          This results in



          Before a=10 b=20 c=30 d=40 e=50
          After a= b= c= d=40 e=50





          share|improve this answer















          This should be a simple loop



          #!/bin/bash

          UNSET=(a b c)

          a=10 b=20 c=30 d=40 e=50

          echo Before a=$a b=$b c=$c d=$d e=$e

          for i in $UNSET[@]
          do
          unset $i
          done

          echo After a=$a b=$b c=$c d=$d e=$e


          This results in



          Before a=10 b=20 c=30 d=40 e=50
          After a= b= c= d=40 e=50






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 9 '18 at 0:42

























          answered Oct 9 '18 at 0:36









          Stephen HarrisStephen Harris

          27.5k35383




          27.5k35383












          • Looks good, works well. Tyvm

            – chamunks
            Oct 9 '18 at 1:01

















          • Looks good, works well. Tyvm

            – chamunks
            Oct 9 '18 at 1:01
















          Looks good, works well. Tyvm

          – chamunks
          Oct 9 '18 at 1:01





          Looks good, works well. Tyvm

          – chamunks
          Oct 9 '18 at 1:01













          0














          You can dereference variables with !:



          $ foo=bar
          $ bar=1
          $ echo $!foo
          1


          You can't use a variable inside an expansion like that though, so you will probably have to do something really ugly with eval.






          share|improve this answer


















          • 1





            Hmm so, I'm not sure how this answers the question precisely. I'm looking to use the strings in the arrays as variable names. Which in the for loop I'm trying to dereference. Pardon my ignorance here I'm just a bit confused as to how to apply it. Also, I appreciate the response and gave an upvote but apparently I'm too new for it to count. Sorry </3

            – chamunks
            Oct 9 '18 at 0:11
















          0














          You can dereference variables with !:



          $ foo=bar
          $ bar=1
          $ echo $!foo
          1


          You can't use a variable inside an expansion like that though, so you will probably have to do something really ugly with eval.






          share|improve this answer


















          • 1





            Hmm so, I'm not sure how this answers the question precisely. I'm looking to use the strings in the arrays as variable names. Which in the for loop I'm trying to dereference. Pardon my ignorance here I'm just a bit confused as to how to apply it. Also, I appreciate the response and gave an upvote but apparently I'm too new for it to count. Sorry </3

            – chamunks
            Oct 9 '18 at 0:11














          0












          0








          0







          You can dereference variables with !:



          $ foo=bar
          $ bar=1
          $ echo $!foo
          1


          You can't use a variable inside an expansion like that though, so you will probably have to do something really ugly with eval.






          share|improve this answer













          You can dereference variables with !:



          $ foo=bar
          $ bar=1
          $ echo $!foo
          1


          You can't use a variable inside an expansion like that though, so you will probably have to do something really ugly with eval.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 8 '18 at 23:53









          DopeGhotiDopeGhoti

          47.1k56191




          47.1k56191







          • 1





            Hmm so, I'm not sure how this answers the question precisely. I'm looking to use the strings in the arrays as variable names. Which in the for loop I'm trying to dereference. Pardon my ignorance here I'm just a bit confused as to how to apply it. Also, I appreciate the response and gave an upvote but apparently I'm too new for it to count. Sorry </3

            – chamunks
            Oct 9 '18 at 0:11













          • 1





            Hmm so, I'm not sure how this answers the question precisely. I'm looking to use the strings in the arrays as variable names. Which in the for loop I'm trying to dereference. Pardon my ignorance here I'm just a bit confused as to how to apply it. Also, I appreciate the response and gave an upvote but apparently I'm too new for it to count. Sorry </3

            – chamunks
            Oct 9 '18 at 0:11








          1




          1





          Hmm so, I'm not sure how this answers the question precisely. I'm looking to use the strings in the arrays as variable names. Which in the for loop I'm trying to dereference. Pardon my ignorance here I'm just a bit confused as to how to apply it. Also, I appreciate the response and gave an upvote but apparently I'm too new for it to count. Sorry </3

          – chamunks
          Oct 9 '18 at 0:11






          Hmm so, I'm not sure how this answers the question precisely. I'm looking to use the strings in the arrays as variable names. Which in the for loop I'm trying to dereference. Pardon my ignorance here I'm just a bit confused as to how to apply it. Also, I appreciate the response and gave an upvote but apparently I'm too new for it to count. Sorry </3

          – chamunks
          Oct 9 '18 at 0:11












          0














          I think you've worked yourself into a circle; simply loop over the array and unset the parameter referred to by the loop:



          AWS_SECRET='secret_here'
          echo Before:
          declare -p AWS_SECRET
          set|grep AWS_SECRET

          for secret in "$CONFIG_SECRETS[@]"
          do
          unset $secret
          done

          echo After:
          declare -p AWS_SECRET
          set|grep AWS_SECRET





          share|improve this answer























          • I feel like I've worked myself into a circle at this point :P Although I wonder if by your suggestion I would just be unsetting $secret and not the environment variable represented by $secret

            – chamunks
            Oct 9 '18 at 0:39











          • That's why I set up a sample variable (AWS_SECRET), to confirm that it's set before the loop and not set after the loop -- test it and see!

            – Jeff Schaller
            Oct 9 '18 at 0:40











          • Also, keep in mind that the variable name is defined as a string within in an array and not your normal way so the example should contain a dynamically referenced string if possible. as its the problem that I'm having, unsetting parameters normally is much easier than doing what I'm doing.

            – chamunks
            Oct 9 '18 at 0:45















          0














          I think you've worked yourself into a circle; simply loop over the array and unset the parameter referred to by the loop:



          AWS_SECRET='secret_here'
          echo Before:
          declare -p AWS_SECRET
          set|grep AWS_SECRET

          for secret in "$CONFIG_SECRETS[@]"
          do
          unset $secret
          done

          echo After:
          declare -p AWS_SECRET
          set|grep AWS_SECRET





          share|improve this answer























          • I feel like I've worked myself into a circle at this point :P Although I wonder if by your suggestion I would just be unsetting $secret and not the environment variable represented by $secret

            – chamunks
            Oct 9 '18 at 0:39











          • That's why I set up a sample variable (AWS_SECRET), to confirm that it's set before the loop and not set after the loop -- test it and see!

            – Jeff Schaller
            Oct 9 '18 at 0:40











          • Also, keep in mind that the variable name is defined as a string within in an array and not your normal way so the example should contain a dynamically referenced string if possible. as its the problem that I'm having, unsetting parameters normally is much easier than doing what I'm doing.

            – chamunks
            Oct 9 '18 at 0:45













          0












          0








          0







          I think you've worked yourself into a circle; simply loop over the array and unset the parameter referred to by the loop:



          AWS_SECRET='secret_here'
          echo Before:
          declare -p AWS_SECRET
          set|grep AWS_SECRET

          for secret in "$CONFIG_SECRETS[@]"
          do
          unset $secret
          done

          echo After:
          declare -p AWS_SECRET
          set|grep AWS_SECRET





          share|improve this answer













          I think you've worked yourself into a circle; simply loop over the array and unset the parameter referred to by the loop:



          AWS_SECRET='secret_here'
          echo Before:
          declare -p AWS_SECRET
          set|grep AWS_SECRET

          for secret in "$CONFIG_SECRETS[@]"
          do
          unset $secret
          done

          echo After:
          declare -p AWS_SECRET
          set|grep AWS_SECRET






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 9 '18 at 0:35









          Jeff SchallerJeff Schaller

          45k1164147




          45k1164147












          • I feel like I've worked myself into a circle at this point :P Although I wonder if by your suggestion I would just be unsetting $secret and not the environment variable represented by $secret

            – chamunks
            Oct 9 '18 at 0:39











          • That's why I set up a sample variable (AWS_SECRET), to confirm that it's set before the loop and not set after the loop -- test it and see!

            – Jeff Schaller
            Oct 9 '18 at 0:40











          • Also, keep in mind that the variable name is defined as a string within in an array and not your normal way so the example should contain a dynamically referenced string if possible. as its the problem that I'm having, unsetting parameters normally is much easier than doing what I'm doing.

            – chamunks
            Oct 9 '18 at 0:45

















          • I feel like I've worked myself into a circle at this point :P Although I wonder if by your suggestion I would just be unsetting $secret and not the environment variable represented by $secret

            – chamunks
            Oct 9 '18 at 0:39











          • That's why I set up a sample variable (AWS_SECRET), to confirm that it's set before the loop and not set after the loop -- test it and see!

            – Jeff Schaller
            Oct 9 '18 at 0:40











          • Also, keep in mind that the variable name is defined as a string within in an array and not your normal way so the example should contain a dynamically referenced string if possible. as its the problem that I'm having, unsetting parameters normally is much easier than doing what I'm doing.

            – chamunks
            Oct 9 '18 at 0:45
















          I feel like I've worked myself into a circle at this point :P Although I wonder if by your suggestion I would just be unsetting $secret and not the environment variable represented by $secret

          – chamunks
          Oct 9 '18 at 0:39





          I feel like I've worked myself into a circle at this point :P Although I wonder if by your suggestion I would just be unsetting $secret and not the environment variable represented by $secret

          – chamunks
          Oct 9 '18 at 0:39













          That's why I set up a sample variable (AWS_SECRET), to confirm that it's set before the loop and not set after the loop -- test it and see!

          – Jeff Schaller
          Oct 9 '18 at 0:40





          That's why I set up a sample variable (AWS_SECRET), to confirm that it's set before the loop and not set after the loop -- test it and see!

          – Jeff Schaller
          Oct 9 '18 at 0:40













          Also, keep in mind that the variable name is defined as a string within in an array and not your normal way so the example should contain a dynamically referenced string if possible. as its the problem that I'm having, unsetting parameters normally is much easier than doing what I'm doing.

          – chamunks
          Oct 9 '18 at 0:45





          Also, keep in mind that the variable name is defined as a string within in an array and not your normal way so the example should contain a dynamically referenced string if possible. as its the problem that I'm having, unsetting parameters normally is much easier than doing what I'm doing.

          – chamunks
          Oct 9 '18 at 0:45

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474097%2fi-want-to-unset-a-list-of-bash-variables-that-have-their-variable-strings-stored%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          -bash, docker, environment-variables

          Popular posts from this blog

          Frič See also Navigation menuinternal link

          Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

          fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox