How to hide all command output with zsh and bash

6,006

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
    
Share:
6,006

Related videos on Youtube

user123456
Author by

user123456

Updated on September 18, 2022

Comments

  • user123456
    user123456 over 1 year

    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.
  • Lucas
    Lucas almost 3 years
    this doesn’t appear to work with Zsh’s builtin >/dev/null 2>&1 or with builtin does_not_exist >/dev/null 2>&1
  • Adaephon
    Adaephon almost 3 years
    @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.