37

Why do many commands provide the option -q or --quiet to suppress output when you can easily achieve the same thing by redirecting standard output to the null file?

  • 3
    I consider that mostly as a convenience flag. – Janis Mar 19 '15 at 16:51
  • 7
    Not all commands use -q to suppress all output. For example, Docker has a command to list images; docker images shows a formatted table with a bunch of information, while docker images -q outputs a plain list of image IDs (useful for piping to other commands). – new123456 Mar 19 '15 at 20:42
70
  • While you can easily redirect in a shell, there are other contexts where it's not as easy, like when executing the command in another language without using a shell command-line. Even in a shell:

    find . -type f -exec grep -q foo {} \; -printf '%s\n'
    

    to print the size of all the files that contain foo. If you redirect to /dev/null, you lose both find and grep output. You'd need to resort to -exec sh -c 'exec grep foo "$1" > /dev/null' sh {} \; (that is, spawn an extra shell).

  • grep -q foo is shorter to type than grep foo > /dev/null
  • Redirecting to /dev/null means the output is still written and then discarded, that's less efficient than not writing it (and not allocate, prepare that output to be written)
  • that allows further optimisations. In the case of grep for instance, since with -q, grep knows the output is not required, it exits as soon as it finds the first match. With grep > /dev/null, it would still try to find all the matches.
  • quiet doesn't necessarily mean silent. For some commands, it means reduce verbosity (the opposite of -v|--verbose). For instance, mplayer has a --quiet and a --really-quiet. With some commands, you can use -qqq to decrease verbosity 3 times.
  • It's noteworthy that in the grep man-page both options, -q, and -s, are suggested to be avoided if portability is a concern. grep's early exit is an important optimization, but not all programs with such options can do that; curl -s will still process the whole data (but the 3rd point still applies, thus saving processing time). – Janis Mar 19 '15 at 17:12
  • 6
    @Janis, that would be portability to very old systems. Both -q and -s are specified by POSIX. If we're to consider systems that old, then my sh -c '...' sh {} \; won't work either (with earlier ksh-based shs). – Stéphane Chazelas Mar 19 '15 at 17:17
  • The man page mentions: USG-style grep also lacked -q but its -s option behaved like GNU grep. - I thought that the difference in behaviour could be an issue. I can't judge about its relevance. – Janis Mar 19 '15 at 17:31
  • I frequently use the -q and --quiet options when I only care about the return code of a command in a script. The reasons noted above are even better examples why. – Mark Stewart Mar 19 '15 at 17:57
  • @StéphaneChazelas: you'd be amazed how (parts of) many systems are still that old... (well, actually, not that many ^^ but there are still some) – Olivier Dulac Mar 19 '15 at 18:28
3

It still allows for the command to put out when It feels it needs to a note to the screen but normally it puts nothing out. with redirecting all to null there is no chance for the output to be seen.

  • Well, quiet means that no response is expected; it would be unexpected if we'd see output anyway. (In case that there's anyway something "really really really" important to print (in a terminal attached process) a tty/pty device could directly be used by the process. An example for that may be interactive inquiry of a password, or somesuch.) – Janis Mar 19 '15 at 16:57
2

Although it probably depends on the command, -q would also disable output to stderr. This allows shells that don't easily allow duping stderr to stdout (I'm looking at you, csh & tcsh) to avoid the noise.

Some programs provide a "quiet" option for situations where it may have other meaningful output that you don't want cluttered with status messages. The -q or equivalent would silence all program status output, allowing a pure data stream from, say, tar or gzip.

0

Besides all the good responses and points above. It is a best practice to provide an opposite option for every flag. So, if there is a -v for verbosity there should be -q for non-verbosity, quite or silence. (this is not the point of discussion but ...) Same as should be a long option for every short one, -q and --quite.

As in almost everything in our business, there are many ways to get a solution or the same results. Having -q is one of them.

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.