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

Shellscript(Bash/Zsh)
2023-01-25 09:13 (2 years ago) ytyng

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.

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

Comments

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011