Setting Proxy Environment Variables on Linux and macOS

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:

bash-3.2$ env

The environment variables are printed in the format <key>=<value>, one per line.

bash-3.2$ env

To view a specific environment variable, use printenv followed by the variable name, for example:

bash-3.2$ printenvenv TERM_PROGRAM

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), use the export shell command and http_proxy=<HOSTNAME>:<PORT>.


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…/escapingsection.html.

export http_proxy=”http://ads\\stefan:Oak%[email protected]:88″

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 %40. If you need to set a proxy for other network protocols such as https and ftp, set it using the variable names https_proxy and ftp_proxy.


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.

bash-3.2$ curl
<!doctype html>
  <html itemscope=”” itemtype=”” 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:

bash-3.2$ env -u ftp_proxy

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.