Linux – How to get the error code (exit code) of “xdg-mime query filetype” command

linuxmime-types

I ran xdg-mime query filetype <file name> to check the MIME type of a file, and it failed. How can I print the error code (exit code) of the xdg-mime command?

I want to know what error happened:

  1. Error in command line syntax.
  2. One of the files passed on the command line did not exist.
  3. A required tool could not be found.
  4. The action failed.
  5. No permission to read one of the files passed on the command line.

Best Answer

In Bourne-derived shells (sh, ash, bash, dash, zsh...) the exit code of the last-run program is in the $? variable:

$ ls /no-such-file
ls: /no-such-file: No such file or directory
$ echo $?
2

So in this case, the exit code of ls is 2.

Related Question