I need to check if mysql is installed from within a bash script. What would be an efficient way of doing that? I thought I could do this:

if [ ! -f `which mysql` ] ; then
    echo "foo" 

But I think I might be confused about something. What would be a better solution? It should be portable and work on both Ubuntu and OSX.

  • What exactly confuses you about the code you posted? What did you expect it to do, and what does it do instead?
    – kopischke
    May 26, 2012 at 19:47
  • which only returns regular files anyway. This test will always be false.
    – Chris Down
    May 26, 2012 at 20:16
  • @kopischke I expected which mysql to give back the path to mysql that then is checked if it's a file or not. But it seems to just not work.
    – Kit Sunde
    May 26, 2012 at 20:19
  • This will also potentially break in any shell where [ is an external command, as if which does not return anything, -f will be missing an argument.
    – Chris Down
    May 26, 2012 at 20:24
  • Also, using which will fail if mysql is an alias.
    – George M
    May 26, 2012 at 21:25

3 Answers 3

type mysql >/dev/null 2>&1 && echo "MySQL present." || echo "MySQL not present."
  • 1
    What is the problem with searching only in $PATH? If it is installed in a non-standard location you need to know that location to run it anyway. And if you know the non-standard location it probably doesn't make much sense to run a script that checks if it is installed or not.
    – Bram
    May 26, 2012 at 21:16
  • @Bram Being installed does not implicitly mean it's going to be in a standard location. What if, as root, I want to know how many mysqld binaries users have compiled and installed on the system? It could be in /opt (not usually considered standard), could be under some user /home directory, could be in a subdirectory under /usr/local. There could also be multiple versions of mysqld installed. Assuming the binary is going to be in $PATH or it's irrelevant is not, in my opinion, a good view to have.
    – George M
    May 26, 2012 at 21:36
  • @Bram - I have absolutely no idea. I'm just providing an alternative for uther's answer.
    – Chris Down
    May 26, 2012 at 22:14
  • I do not recommend running find over the entire filesystem. It's rude. You may be traversing external drives, network mounts, … If mysql isn't in the path, it might as well not be installed. There might be a non-working installation of mysql that's not in the path on purpose. May 26, 2012 at 23:47

If you exclusively use your Ubuntu package manager for MySQL installation, then you can just use the dpkg front-end to dpkg-query to find out whether the package is installed.

# Look up package with dpkg-query.
if dpkg --list mysql-client | egrep -q ^ii; then

This is fast and reliable, but obviously only works when you use your Ubuntu system packages.


I wrote a Rightscale script and needed to know if mysql-server was installed. I used:

if [ -f /etc/init.d/mysql* ]; then
    echo "installed"
    echo "not installed"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.