Linux – What’s the best way to check if a volume is mounted in a Bash script

bashlinuxmount

What's the best way to check if a volume is mounted in a Bash script?

What I'd really like is a method that I can use like this:

if <something is mounted at /mnt/foo> 
then
   <Do some stuff>
else
   <Do some different stuff>
fi

Best Answer

Avoid using /etc/mtab because it may be inconsistent.

Avoid piping mount because it needn't be that complicated.

Simply:

if grep -qs '/mnt/foo ' /proc/mounts; then
    echo "It's mounted."
else
    echo "It's not mounted."
fi

(The space after the /mnt/foo is to avoid matching e.g. /mnt/foo-bar.)