How to Check if a Volume Is Mounted in Bash


If you need to check if a volume is mounted in a Bash script, then you can do the following.

How to Check Mounted Volumes

First we need to determine the command that will be able to check.

This can be done with the /proc/mounts path.

How to Check if a Volume is Mounted in Bash

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

How to Check if a Volume is Mounted and Available

MNT_DIR=/mnt/foo
df_result=$(timeout 10 df "$MNT_DIR")
[[ $df_result =~ $MNT_DIR ]] 
if [ "$BASH_REMATCH" = "$MNT_DIR" ]
then
    echo "It's available."
else
    echo "It's not available."
fi

Another way of Checking Volume Mounts

mount \
    | cut -f 3 -d ' ' \
    | grep -q /mnt/foo \
  && echo "mounted" || echo "not mounted"