Unable to change environment variables in bash script

Hello!

For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which will be typed by me on the keyboard).

So if my password is 'Linda' the script has to change for instance the environment variable http_proxy from

myuser:PASSWORD

to

myuser:Linda

Could you please help me to fix the script below? There are two lines that do not work (indicated in the script). I have already tried many combinations of eval, \$$, etc...

Thanks a lot!
Marko

#!/bin/bash
read -s -p "Password: " passw; echo
perl_cmd="s/PASSWORD/$passw/g"
echo "perl_cmd = $perl_cmd"
#for var in `set | grep PASSWORD`; do  # fails if there are spaces in the value
for var in `set | perl -lne 'print $1 if /(.*?)=.*PASSWORD/'`; do  # works fine
         echo "var = $var"
         val=`echo $var | perl -pe '$perl_cmd'`  # does not work
         echo "val = $val"
         export $var=$val   # does not work
done

Should

export $var=$val   # does not work

not be

export var=$val   # does not work

Correct me if I am wrong. I am learning as well!

Hi dahlia,

The problem of what you propose is that I don't want to set a variable called "var" but instead a variable whose name is the value of "var".

Thanks!
Marko

It is too difficult to guess what you are trying to achive from script which "does not work".
Please give a detailed example.

Sorry, I realise now that my post was not clear enough.

All I want is be able to use the string "PASSWORD" instead of my actual password in the export commands inside my .bashrc and to change these variables replacing "PASSWORD" by the actual password by using a script. In that way my password is not written in the disk.

I just found a way to do it. Here is it:

read -s -p "Password: " passw; echo
for var in `set | perl -lne 'print $1 if /(.*?)=.*PASSWORD/'`; do
    eval val=\${$var//PASSWORD/$passw}
    #echo "val = $val"
    eval export $var=\'$val\'
done

The for line is kind of ugly, but I couldn't find a more elegant way of looping over all environment variables. Anyway, it works and I am happy with it.

The final difficulty I had with it is that if I put "#!/bin/bash" in the beginning and call the script as a sub shell I don't get the exports... The solution was to add

alias set-password='source $HOME/bin/set-password.sh'

to my .bashrc, to avoid having to type the source command.

Thanks a lot everybody!
Marko