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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
You have a funny dangling ; then in your script. – Mat May 17 '12 at 16:18
up vote 9 down vote accepted

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.

share|improve this answer
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. – uther 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
@ChrisDown "Word splitting will eat your babies if you don't quote things properly." Words to live by. – uther 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"
share|improve this answer
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

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.