Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
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 '12 at 19:47
which only returns regular files anyway. This test will always be false. – Chris Down May 26 '12 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 '12 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 '12 at 20:24
Also, using which will fail if mysql is an alias. – uther May 26 '12 at 21:25

3 Answers 3

up vote 4 down vote accepted
type mysql >/dev/null 2>&1 && echo "MySQL present." || echo "MySQL not present."
share|improve this answer
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 '12 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. – uther May 26 '12 at 21:36
@Bram - I have absolutely no idea. I'm just providing an alternative for uther's answer. – Chris Down May 26 '12 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. – Gilles May 26 '12 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.

share|improve this answer

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"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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