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

          Word for a person who has no opinion about whether god existsWord for having a definite opinion while simultaneously withholding judgment?What's the opposite of “newcomer? Is ”veteran" OK?What do you call an “atheist” who might believe in an afterlife?What's a word for someone who wants to voice opinions but not have them challenged?Word for someone who dismisses contrary opinions as irrational?Somone who thinks they are overly special/out of the ordinaryIs there a word, phrase or idiom for “a person who is incapable of thinking about the future”?The belief that a god is human-likeA word for a non-famous person/thing you have heard a lot aboutAdjective for a person who enjoys taking care of their appearance

          What was this official D&D 3.5e Lovecraft-flavored rulebook?What was this set of RPG tools called?As a first-time DM should I let my players play complex character classes and roles?Nymph's Kiss and the RelationshipWhat was the name of this Cleric Prestige Class that shapes metal with its bare hands?Are the 3.5e Dragonlance books third party or official works?What's up with the domain Vile Darkness?What was this 80s book about RPGs?What was the name of this Werewolf band?What book had Rituals to “upgrade” animal companions to keep them viable at higher levels?What was this RPG that had rules for player-owned businesses?

          2017 IndyCar Series Contents Series news Teams and drivers Schedule Season summary Footnotes References External links Navigation menu"INDYCAR: Initial 2018 bodywork concepts unveiled"the original"IndyCar confirms switch to Performance Friction brakes in 2017""AJ Foyt Racing will switch to Chevy"the original"Carlos Munoz, Conor Daly will drive for AJ Foyt Racing""Zach Veach's Indy 500 Debut Confirmed with Foyt""No mass exodus from Honda after Ganassi switch""Ex-F1 driver Sato joins Andretti Autosport for 2017 IndyCar season""IndyCar's Ryan Hunter-Reay, sponsor DHL paired through 2020""hhgregg and Andretti Autosport announce partnership for key races in 2016""INDYCAR: Rossi re-signs with Andretti"the original"McLaren Formula 1 - Fernando Alonso to race at Indy 500 with McLaren, Honda and Andretti Autosport""Shank will finally take part in Indy 500 with Harvey, Andretti | MotorSportsTalk""Andretti adds Jack Harvey to Indy 500 field""Ganassi switches to Honda power for 2017""INDYCAR: Chilton returns to Ganassi"the original"IndyCar silly season: Who's going where in 2017?""INDYCAR: Kanaan, NTT Data return to Ganassi"the original"Kimball to remain at Ganassi for 2017""Coyne confirms Bourdais for 2017 IndyCar season""Davison to sub for Bourdais in Indy 500"the original"Gutierrez confirmed for Detroit IndyCar debut""Gutierrez returns with Coyne for rest of 2017 season""Vautier to drive for Coyne at Texas"the original"INDYCAR: Coyne confirms Jones for 2017"the original"Pippa Mann returns to Coyne for Indy 500""Karam, Dreyer & Reinbold teaming up again for Indianapolis 500""Pigot to return to Ed Carpenter Racing""Hildebrand confirmed as full-time Ed Carpenter driver""Veach to replace injured Hildebrand at Barber"the originalNew Team Harding Racing Enters Chaves for 101st Indianapolis 500"Juncos Racing Announces Entry in 101st Running of the Indianapolis 500 :: Juncos Racing""Juncos confirms Pigot for Indy 500""Saavedra confirmed in Juncos' second 500 entry"the original"Lazier confirms Indy 500 run after son's USF2000 debut"the original"Claman DeMelo to race for RLLR at Sonoma"the original"Rahal signs Servia and ace engineer for 2017""IndyCar: Aleshin returns with Schmidt"the original"Aleshin replaced by Saavedra for Toronto""Jack Harvey will pilot SPM No. 7 car at Watkins Glen, Sonoma""Jay Howard confirmed in Tony Stewart's supported SPM Indy entry""INDYCAR: Newgarden to wave the flag at Penske"the original"Pagenaud opts for No. 1 in 2017"the original"Penske confirms Newgarden for 2017""Montoya to stay with Team Penske in 2017""Target leaving IndyCar after 27 seasons with Chip Ganassi""Cavin: IndyCar could see complete driver/team shakeup in 2017""End of the road for KV Racing?""KV Racing confirms closure, equipment sold to Juncos""Juncos confirms IndyCar Series entry"the original"Juncos readies IndyCar program, aims for '17 500"the original"Harding Racing to add Texas, Pocono to schedule"the original"Sato signs with Andretti Autosport for 2017""INDYCAR: Aleshin in Doubt at SPM"the original"Long Beach notebook: JR Hildebrand breaks hand""Hildebrand cleared to return at Phoenix"the original"Bourdais to undergo surgery on multiple fractures""Aleshin loses Schmidt Peterson IndyCar ride""Saavedra in at SPM for Pocono, Gateway"the original"Bourdais to make return at Gateway"the original"The IndyCar Grand Prix no longer is sponsored by Angie's List""2017 IndyCar Series rulebook""2017 Verizon IndyCar Series Official Rulebook"Official websiteeeeee