MacOS – Switching between proxy and no-proxy config in .bash_profile

bashmacosPROXYterminal

I have configured proxy settings (on mavericks) within $HOME/.bash_profile (purpose: to make npm, rvm, git use the corporate proxy) and the configs are as follows:

git config --global http.proxy http://proxy_usr:proxy_pwd@proxy_ipaddress:proxy_port
git config --global http.proxy https://proxy_usr:proxy_pwd@proxy_ipaddress:proxy_port
export http_proxy=http://proxy_usr:proxy_pwd@proxy_ipaddress:proxy_port
export ALL_PROXY=$http_proxy
npm config set proxy http://proxy_usr:proxy_pwd@proxy_ipaddress:proxy_port
npm config set https-proxy http://proxy_usr:proxy_pwd@proxy_ipaddress:proxy_port

However, I don't need these configurations at home.

What is the best way to manage the .bash_profile so that my mac should pick the settings based on the proxy? Can we write a shell script to set my .bash_profile based on network I use (home/ work – no-proxy/proxy)?

Best Answer

You may create a separate shell script and source it whenever you are at a "non-proxified" location. For example, I have a .unset_proxy.sh under my user directory that I source whenever I need to get work done at home or starbucks. This is what it looks like:

unset HTTP_PROXY
unset HTTPS_PROXY
unset http_proxy
unset https_proxy
npm config delete https-proxy
npm config delete http-proxy
npm config delete proxy

This is the command I run: . ~/.unset_proxy

When I'm back at the office building I simply source my bash profile to reset the proxy: . ~/.bash_profile