43

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?

2
  • 3
    I consider that mostly as a convenience flag.
    – Janis
    Mar 19, 2015 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, 2015 at 20:42

4 Answers 4

75
  • 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.
11
  • 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, 2015 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). Mar 19, 2015 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, 2015 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. Mar 19, 2015 at 17:57
  • 1
    @Janis - it may be that the GNU grep manpage warning of -q portability concerns has another implication. Some readings of POSIX grep pages, when considered alongside Utility Description Defaults might imply that { grep -q pattern; head; } <seekable_file would print only the 10 lines following the first match for pattern. GNU grep won't do that (not even when combined with its non-POSIX -m1).
    – mikeserv
    Mar 20, 2015 at 14:45
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.

1
  • 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, 2015 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 -v for verbosity there should be -q for non-verbosity, quiet or silence. (This is not the point of discussion though...) There should be a long option for every short one as well, -q and --quiet.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .