Bash supports environment variables that control how commands operate within a session. Many network-aware utilities, such as curl or wget, use proxy variables to redirect traffic through a specific gateway. Correctly configuring these variables ensures consistent and automated proxy usage for all relevant commands on a system.

Setting a proxy variable can be done temporarily or permanently. Temporary assignment affects only the current session or terminal, which is ideal for short tasks. Permanent configuration modifies shell startup files, enabling the same settings each time a new session is opened.

Using a proxy may be necessary if operating behind a firewall, on a corporate network, or during security tests. Trusted proxies can improve privacy by masking source IP addresses. A well-configured HTTP_PROXY or HTTPS_PROXY ensures seamless access to web resources in restricted or monitored environments.

Steps to set a proxy environment variable for the current session

Configuring a proxy for the current session applies only to the active terminal until it is closed, which is useful for testing or quick changes. This method does not require modifying shell startup files and will not persist across new sessions. Verify these changes in the same terminal where they were applied.

  1. Identify the proxy server details, including protocol, host, and port.
  2. Define the HTTP_PROXY variable using the export command, adjusting credentials and port as needed.
  3. Confirm the setting by printing the variable.
    $ export HTTP_PROXY=http://proxy.example.com:8080
    $ echo $HTTP_PROXY
    http://proxy.example.com:8080
  4. Repeat the process for HTTPS_PROXY if required.
  5. Test connectivity using curl or any other network utility.
    $ curl --verbose http://example.com
    *   Trying proxy.example.com...
    * Connected to proxy.example.com (10.0.0.1) port 8080
    ...

    Use similar steps for FTP_PROXY or other protocols as needed.

  6. Unset the proxy variable to restore direct access if no longer needed.
    $ unset HTTP_PROXY
    $ echo $HTTP_PROXY
    

    Run ‘unset HTTPS_PROXY’ if an HTTPS proxy was also configured.

Steps to set a proxy environment variable persistently

For continuous or repeated usage, setting proxy variables in a shell startup file ensures they remain configured in every new session. Typical files include ~/.bashrc or ~/.bash_profile, but system-wide files such as /etc/environment can also be updated. Choose the file that best suits your usage and administrative privileges.

  1. Open the desired shell startup file with a text editor.
  2. Add the export commands for HTTP_PROXY and HTTPS_PROXY to the file.
    # Example entries in ~/.bashrc
    export HTTP_PROXY=http://proxy.example.com:8080
    export HTTPS_PROXY=http://proxy.example.com:8080

    Storing credentials in plain text may pose security risks. Consider secure credential management when adding authentication details.

  3. Save the file and reload it.
    $ source ~/.bashrc
  4. Verify the proxy configuration by checking the variables in a new shell.
    $ echo $HTTP_PROXY
    http://proxy.example.com:8080

    Include no_proxy to bypass specific domains or IP addresses.

  5. Test the proxy as needed using curl or wget.
Discuss the article:

Comment anonymously. Login not required.