Sometimes you need to go to the internets through a proxy with authorization. And even in terminal on OS X.
First, you configure the proxy information in System Preferences / Network / Advanced / Proxies. There are Web Proxy Server, Username, and Password fields you fill in. It saves the credentials in the login keychain, so it makes sense to use them instead of typing them plaintext in terminal. To access the keychain, security program is used.
Having combined the command to set up proxy and get the credentials, here’s what I got:
In the script above I added backslashes to split the command into multiple lines. It should work this way, but if it doesn’t remove the backslashes and merge the lines.
The script assumes the name of the item with the saved password is proxy.example.com, which is the address of your proxy and you should change it.
Note also the $(printf $(security …)) construction. Because in my case the username has the format domain\user, the security app returns domain\134user, where \134 is backslash escaping of \. printf converts it to the character itself. It may not be secure in general case!
NB! If you issue a command like echo "$https_proxy", it’ll show your username and password in plaintext. I haven’t figured out a way to avoid that yet.