How to Integrate MacOS Terminal with Clash

When working with Clash, a stable proxy in Global mode may not be enough to prevent network issues when using commands such as Homebrew, git, or npm. These issues can be incredibly time-consuming to resolve and may recur in the future due to the limitations of using alternative source mirrors. In this article, we will explore two different approaches that can completely resolve network issues and provide a more seamless experience.


Preparation:

To get started, open the dashboard of Clash and adjust your preferences as follows:

Suggested Clash Setting
Suggested Clash Setting

Approach 1 (manual, not suggested):

One way to tell the endpoint to use a specific proxy is by setting the http_proxy and https_proxy environment variables. This can be done by executing the following configuration script directly in the terminal. Please note that this method is not recommended as it will take effect only temporarily:

export http_proxy=http://127.0.0.1:7890export https_proxy=http://127.0.0.1:7890

Approach 2 (automated):

The second approach is to use a script that includes two functions: proxy_on and proxy_off. This script allows you to enable or disable proxying with a simple command rather than manually setting the environment variables. Here is an example of such a script:

function proxy_on() {    export http_proxy=http://127.0.0.1:7890    export https_proxy=http://127.0.0.1:7890    echo -e "Terminal Proxying Enabled"}​function proxy_off(){    unset http_proxy https_proxy    echo -e "Terminal Proxying Disabled"}

To install this script into your command-line tools, use the following command to determine which shell you are using:

echo $SHELL

If the result is /bin/bash, then add the script to ~/.bash_profile. If the result is /bin/zsh, then add the script to ~/.zprofile. Once the script is in place, run the following command to make it available in your current session:

source ~/.bash_profile

or

source ~/.zprofile

Done!

Once the script is installed, you can enable Terminal Proxying with the command:

or disable it with the command:


Verification:

To verify that the proxying is working correctly, you can use the following command:

curl -I https://www.google.com

This should return the headers of the Google homepage. If you receive any errors or are unable to connect, please double-check your configuration and make sure that the proxy is enabled.


In conclusion, these two approaches provide different ways to resolve network issues when using Clash. The first approach is manual and temporary, while the second approach is scripted and more permanent. By using the second approach, you can easily enable and disable Terminal Proxying as needed.