4

How can I hide all output that is written and outputted in a terminal?

In other words, I am looking to add the string

>/dev/null 2&>1

to every command I write.

  • How would you do it with bash?
  • How would you do it with zsh?
  • Ideally how to have a configuration that take into account any terminal.
6

zsh:

You can redirect stdout and stderr of any following command to /dev/null by running these two command:

exec >/dev/null
exec 2>/dev/null

Note: This will still show the prompt and anything you type on the command line, but not much else.

bash:

You can redirect stdout and stderr with the following command

exec >/dev/null 2>&1

Note

  • This may suppress any output, including the prompt and what you type on the command line. (This was the case for me with zsh 5.2 and bash 4.4, when I first created this answer in 2016. While it seems to still be the case with bash 5.1.8, it looks like with zsh this is no longer the case since at least version 5.8)

  • To enable the output again, run

      exec >/dev/tty
      exec 2>/dev/tty
    
2
  • this doesn’t appear to work with Zsh’s builtin >/dev/null 2>&1 or with builtin does_not_exist >/dev/null 2>&1
    – Lucas
    May 25 at 14:13
  • @Lucas I just checked with my zsh (5.8) and curiously builtin nx >/dev/null 2>&1 does indeed still produce a "no such builtin" error message, while running nx >/dev/null 2>&1 does suppress the "command not found" error. But running exec 2>/dev/null beforehand does suppress any error messages thereafter, including those of builtin nx (at least on my machine). Running builtin without any arguments does not produce any output for me anyway, with or without redirections. And when running for example builtin pwd >/dev/null 2>&1 the output looks to be suppressed as expected.
    – Adaephon
    Jun 4 at 8:19

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.