I want to check if a USB is busy and store the result in a variable:

$ foo=$(umount /dev/sdb1)
$ umount: /path/mounted: target is busy.

But when I want to print $foo, I get an empty variable

$ echo $foo

My final goal is to umount the USB. If the target is busy, issue a warning to the user, using whiptail. So:

$ [[ -z $foo ]] || whiptail --msbox "Cancel the operation that is maintaining your USB busy" 0 0

Is it possible to store the error target is busy on a variable, or should I use another kind of technique?

  • umount /dev/sdb1 || whiptail should work. – jordanm Sep 6 at 0:19
  • @jordanm Thanks, it seems that works – guillermo chamorro Sep 6 at 0:24

Programs commonly write error messages to the “standard error” I/O stream; “stderr” for short.  If you search for that term, you’ll gets millions of results; the short explanation is that stderr exists so error messages can and will go to the screen when the “standard output” (“stdout”) is redirected, as in a hypothetical command like

umount /dev/sdb1 > um.result.txt

And you can see this happening; the error message appears on your screen even though you are trying to capture it in a variable.

The equally short answer is to use 2>&1 to merge the stderr stream into the stdout stream, so

foo=$(umount /dev/sdb1 2>&1)

will capture the error message in the variable.

Another approach, as suggested by jordanm in a comment, is to look at the exit status.  Variations on this theme include:

umount /dev/sdb1  ||  complain to user
if ! umount /dev/sdb1
    complain to user
umount /dev/sdb1 2> /dev/null  ||  complain to user
 foo=$(umount /dev/sdb1 2>&1)  ||  complain to user using "$foo"

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.