I have created a bash script that is suppose to either append or overwrite some existing environment variables in /etc/environment. The script looks like this:
#!/bin/bash
declare -a array=("http_proxy=http://username:password@proxy:8080"
"https_proxy=http://username:password@proxy:8080"
"ftp_proxy=ftp://username:password@proxy:8080"
"no_proxy=\"localhost, 127.0.0.1\""
)
for i in "${array[@]}"; do
awk -v var="${i%%=*}" '$0 !~ var' /etc/environment > /etc/environment
echo "$i" >> /etc/environment
done
Given the following input (/etc/environment):
http_proxy=http://otheruser:password@otherproxy:8080
https_proxy=http://username:password@proxy:8080
some_env_varible=some_value
I expect the following output:
http_proxy=http://username:password@proxy:8080
https_proxy=http://username:password@proxy:8080
ftp_proxy=ftp://username:password@proxy:8080
no_proxy="localhost, 127.0.0.1"
some_env_variable=some_value
However i get the following output:
http_proxy=http://username:password@proxy:8080
https_proxy=http://username:password@proxy:8080
ftp_proxy=ftp://username:password@proxy:8080
no_proxy="localhost, 127.0.0.1"
Running the awk command outside of the script seems to work fine, returning only the lines that do not match a specific environment variable. I am puzzled to why this is not working inside this script.
Update
As per request:
$ sudo cat /etc/environment
http_proxy=http://username:password@proxy:8080
https_proxy=http://username:password@proxy:8080
ftp_proxy=ftp://username:password@proxy:8080
no_proxy="localhost, 127.0.0.1"
$ i="http_proxy=http://username2:password2@proxy:8080"
$ sudo awk -v var="${i%%=*}" '$0 !~ var' /etc/environment
https_proxy=http://username:password@proxy:8080
ftp_proxy=ftp://username:password@proxy:8080
no_proxy="localhost, 127.0.0.1"