I am new at scripting ... I can do very basic stuff, but now I need a hand.

I have a local filesystem that only will be mounted when I need to do a backup.

I'm starting with this.

export MOUNT=/myfilesystem

if grep -qs $MOUNT /proc/mounts; then
  echo "It's mounted."
  echo "It's not mounted."; then
  mount $MOUNT;

As I said, I'm very basic at scripting. I heard that you can check the status of the mount command by looking at the return codes.

       mount has the following return codes (the bits can be ORed):
       0      success
       1      incorrect invocation or permissions
       2      system error (out of memory, cannot fork, no more loop devices)
       4      internal mount bug
       8      user interrupt
       16     problems writing or locking /etc/mtab
       32     mount failure
       64     some mount succeeded

I don't know how to check that. Any guidance?

  • 2
    You have a funny dangling ; then in your script. – Mat May 17 '12 at 16:18
  • why are we exporting the MOUNT var and also remove the ";" – Mike Q Nov 23 '16 at 21:38
  • Similar questions are on Server Fault, Stack Overflow and Unix & Linux Stack Exchange. – sasha Aug 11 '19 at 22:05

You can check the status code of mount, and most well written executables, with the shell special parameter ?.

From man bash:

? Expands to the exit status of the most recently executed foreground pipeline.

After you run the mount command, immediately executing echo $? will print the status code from the previous command.

# mount /dev/dvd1 /mnt
  mount: no medium found on /dev/sr0
# echo $?

Not all executables have well defined status codes. At a minimum, it should exit with a success (0) or failure (1) code, but that's not always the case.

To expand on (and correct) your example script, I added a nested if construct for clarity. It's not the only way to test the status code and perform an action, but it's the easiest to read when learning.


if grep -qs "$mount" /proc/mounts; then
  echo "It's mounted."
  echo "It's not mounted."
  mount "$mount"
  if [ $? -eq 0 ]; then
   echo "Mount success!"
   echo "Something went wrong with the mount..."

For more information on "Exit and Exit Status", you can refer to the Advanced Bash-Scripting Guide.

|improve this answer|||||
  • 1
    There's no need to export $MOUNT. This script will also break if the filesystem has any character that would undergo wordsplitting (spaces, etc.). You should always quote your expansions. – Chris Down May 17 '12 at 18:49
  • @ChrisDown You're right. I only deleted the obvious dangling ';then'. In the future, feel free to edit my answer to make it more correct. – George M May 17 '12 at 19:30
  • Er... this will still break. You have still not quoted the expansions. On your advice, I'll do it now. – Chris Down May 17 '12 at 20:14
  • I also wouldn't recommend linking to the infamous "Advanced" Bash scripting guide, it's full of errors and will teach people to write bugs, not scripts. BashGuide is a far better alternative. – Chris Down May 17 '12 at 20:15
  • 2
    @ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by. – George M May 17 '12 at 21:25

Many Linux distros have the mountpoint command. It can explicitly used to check if a directory is a mountpoint. Simple as this:

if mountpoint -q "$1"; then
    echo "$1 is a mountpoint"
    echo "$1 is not a mountpoint"
|improve this answer|||||
  • 1
    This approach will fail if you try to check an encfs, since you will stumble into a permission denied error, when run by a non-root user. – Denys S. Dec 1 '15 at 8:40
  • My solution works with encfs. – Theodore R. Smith Mar 22 '18 at 16:13

One more way:

if findmnt ${mount_point}) >/dev/null 2>&1 ; then
  #Do something for positive result (exit 0)
  #Do something for negative result (exit 1)
|improve this answer|||||
  • Hello @sungtm; it appears you logged in with a new, separate account. Since your "Sun" account does not have much activity, I'll suggest that you continue using your new "sungtm" account, as that is simpler. If, however, you'd like to merge the accounts, let me know by flagging this comment and we'll reach out to the Stack Exchange team. Thank you! – Jeff Schaller May 31 '19 at 17:08
  • Note that for encfs, findmnt has to be supplied with the parameter --source encfs, otherwise it will always consider the directory to be mounted because it falls back to the parent mount. – Burkart Jun 16 '19 at 11:01

The easiest way which doesn't require root is:

if $(df | grep -q /mnt/ramdisk); then


or to see if it isn't mounted:

if ! $(df | grep -q /mnt/ramdisk); then

|improve this answer|||||
  • the mount command doesn't need root access, df is focused on disc space not on mount points... – Philippe Gachoud Nov 28 '18 at 11:04

Short statements

Check if mounted:

mount|grep -q "/mnt/data" && echo "/mnt/data is mounted; I can follow my job!"

Check if not mounted:

mount|grep -q "/mnt/data" || echo "/mnt/data is not mounted I could probably mount it!"
|improve this answer|||||

I have tried with below script

echo "enter the file system to check whether its mounted or not"
read p
echo $p
for i in `cat /proc/mounts`
if [[ $p =~ $i ]]
echo "$p is mounted"
echo "$p is not mounted"

Only input you need to give is name of filesystem

|improve this answer|||||

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.