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

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.)

Leave a Comment