When Setting the Path for mysql-client Installed via Homebrew on macOS, It Automatically Follows Version Changes

Shellscript(Bash/Zsh)
2023-01-25 00:13 (3 years ago)
When Setting the Path for mysql-client Installed via Homebrew on macOS, It Automatically Follows Version Changes

When installing mysql-client via Homebrew, you typically add the bin directory to your PATH in .zprofile or .zshrc.

For example:

if [ -d /usr/local/Cellar/mysql-client/8.0.22/bin ]; then
  export PATH=/usr/local/Cellar/mysql-client/8.0.22/bin:${PATH}
fi

However, this approach doesn't handle version changes well. To make the PATH setting adaptable to any version, you can use the following script:

for dir_name in $(ls -1 -r '/usr/local/Cellar/mysql-client'); do
    _path="/usr/local/Cellar/mysql-client/${dir_name}/bin"
    if [ -d "${_path}" ]; then
        export PATH=${_path}:${PATH}
        break
    fi
done

for dir_name in $(ls -1 -r '/usr/local/Cellar/mysql-client'); do
    _path="opt/homebrew/Cellar/mysql-client/${dir_name}/bin"
    if [ -d "${_path}" ]; then
        export PATH=${_path}:${PATH}
        break
    fi
done

This script iterates through the directories in /usr/local/Cellar/mysql-client and /opt/homebrew/Cellar/mysql-client, adding the first valid bin directory it finds to the PATH. This way, your PATH will automatically adjust to the installed version of mysql-client.

Please rate this article (No signup or login required)
Currently unrated

Related posts

The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive