I understand that mountpoint is used to determine if a given directory is a mountpoint or not. However, what I don't understand is its combination of it and the if statement.

I tested a code snippet from the Internet:


if mountpoint -q $1; then
  echo "$1 is mounted"
  echo "$1 isn't mounted"

It works! But I am not sure how it works (especially -q means to be quiet). I tried to put the condition into a pair of double square brackets:

if [[ mountpoint -q $1 ]]; then ...

And it doesn't work. So I want to ask what the internal mechanism is that makes this if statement work?


The mountpoint command will exit with a status zero if the directory or file is a mountpoint, non-zero if not.

That's what the if command is checking for. Whether the command exits successfully (zero status) or not (non-zero status.) (It has nothing to do with whether there's any output or not.)

To explain the last part... The [[ ... ]] construct will check whether there's a string or not, but in that case you would need to capture the output using something like [[ $(mountpoint $1) ]], but something like that would only work if the command would only print output in that case, so something like this would be more realistic [[ $(mountpoint $1 | grep 'is a mountpoint') ]]. But that's inferior... Just use -q and check the exit status directly, that's how this is meant to be used!

|improve this answer|||||
  • 1
    Oh I see, thanks!I have one more question: In fact I suspected that mountpoint -q $1 could output something and thus I tired echo $(mountpoint -q $1) and nothing is output (as expected from the -q option). Now I understand it is exit code rather than its output that matters. Would it be possible to display the exit status in one sentence like "echo -some -options $(mountpoint -q $1)"? (I understand that echo $? would work but that would be at least two sentences) – Mamsds Apr 24 '18 at 5:25
  • @Mamsds Yes, it's possible. For that you need && and || operators. These are just slightly different from an if..else..fi statement, since they work with left-associativity, which means in this command moutpoint -q "$1" && echo "it's a mountpoint" will only echo something if the left-most command succeeds. By that same rule, moutpoint -q "$1" && echo "it's a mountpoint" || echo "Not a mountpoint or echo command failed"` will only echo last thing if the left-most group failed ( imagine having parenthesis around first two commands) – Sergiy Kolodyazhnyy Apr 24 '18 at 5:31
  • @SergiyKolodyazhnyy Hi Sergiy, what I want to show the exit code. For example, if the exit code of (mountpoint -q $1) is 1, I want echo to show me 1 as if it is a normal output. – Mamsds Apr 24 '18 at 6:53
  • @Mamsds This should work: $(mountpoint -q $1; echo $?). Though I can't see why you'd want to do that... Anyways, I hope it helps. – filbranden Apr 24 '18 at 12:38
  • @FilipeBrandenburger Many thanks! Yes it is not practically necessary just I would like to learn how to make echo work on this occasion... – Mamsds Apr 24 '18 at 14:31

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.