I tried to use a bash script to add or remove proxies in bulk, but the script runs with confusing results.
I can add proxies in bulk using the following code. This code is saved by me in use_proxy.sh:
#!/bin/bash
PROXY="http://127.0.0.1:10889"
set_proxy() {
export http_proxy="$PROXY"
export https_proxy="$PROXY"
export HTTP_PROXY="$PROXY"
export HTTPS_PROXY="$PROXY"
echo "代理已设置为 $PROXY"
}
set_proxy
When I run this script (bash use_proxy.sh
) with the bash command, everything works fine, and running the env
command does add the proxy address
But I am not able to remove proxies in bulk using the following code, which is saved in unset_proxy.sh:
#!/bin/bash
unset_proxy() {
unset http_proxy
unset https_proxy
unset HTTP_PROXY
unset HTTPS_PROXY
echo "代理已取消"
}
unset_proxy
When I use this script (i.e. run the bash unset_proxy.sh
), this script does not work. When I run the env
command, I still see the proxy address previously set by use_proxy.sh
Why?
I found that if the script is run using source
(aka source unset_proxy.sh
) it executes successfully.
So now I have a new question: why does bash unset_proxy.sh
fail to unset the environment variable correctly, while source unset_proxy.sh
will successfully unset the environment variable?