When using shell commands that make HTTP requests or tools such as cURL, it may be necessary to use a proxy. The proxy environment variables shown in this post can be set to use a proxy and authentication credentials within the context of the current shell session. I am using macOS and the bash shell here.
View Current Environment Variables
To view all environment variables and their values, use the
env shell command:
The environment variables are printed in the format
<key>=<value>, one per line.
To view a specific environment variable, use
printenv followed by the variable name, for example:
Set Proxy Environment Variables
When using shell commands that make HTTP requests and you are behind a proxy, it is necessary to
set proxy environment variables that provide the proxy’s hostname, port and if applicable,
authentication credentials. For example, cURL uses the proxy environment variables when making HTTP requests.
To set a HTTP proxy and its port for the current shell session (local variable),
export shell command and
If the proxy you need to use requires an active directory domain and user credentials for authentication, you need to use the format
Special characters in values need to be escaped. For details on bash shell escaping, see https://www.tldp.org/…/escapingsection.html.
In the above example, the active directory domain is
http://ads, the username is
stefan and the password is
[email protected]. The backslash between domain and username is escaped with
\\ and the "at" character is escaped using
If you need to set a proxy for other network protocols such as
ftp, set it using the variable names
Now, when making requests using e.g. cURL, the program will use the proxy variables when establishing a connection. In the below example, the html code of Google’s search page is fetched via HTTPS GET.
<html itemscope=”” itemtype=”http://schema.org/WebPage” lang=”en”>
<meta content=”Search the world’s information, including webpages, images, videos and more. Google has many special features to help you find exactly what you’re looking for.” name=”description”>
Unset Proxy Environment Variables
Environment variables can be unset in the bash shell using the
env command and the argument
-u. For example, to unset
ftp_proxy, run the command: