Skip to main content

How to Hide a Command's Output in bash and zsh with > /dev/null

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Some commands print things you don't need, especially inside scripts. Redirecting their output to /dev/null, a special file that throws away everything written to it, keeps the terminal quiet. This guide hides the output of ls, with real output from zsh 5.9.

Syntax​

Syntax
<command> > /dev/null
PartWhat it means
<command>Runs as usual
>Sends the command's output to a file instead of the terminal
/dev/nullThe null device: whatever is written to it is thrown away

The macOS manual page, man null: "The null device accepts and reads data as any ordinary (and willing) file - but throws it away." The bash manual on >: "Redirecting output opens the file whose name results from the expansion of word for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified."

Prerequisites​

  • zsh or bash. This guide uses zsh 5.9, the default shell on macOS, and checks every command in bash 3.2 too.

Step 1: Hide the Output of ls​

The folder holds three empty files. List them, then list them again into /dev/null:

Terminal
ls
ls > /dev/null
Output
draft.txt notes.txt report.txt

zsh running ls, which lists draft.txt, notes.txt and report.txt, then ls > /dev/null, which prints nothing.

The first ls listed the three files. The second ran the same listing, but its output went to /dev/null, so nothing appeared.

Step 2: See That Errors Still Show​

missing.txt doesn't exist:

Terminal
ls missing.txt > /dev/null
Output
ls: missing.txt: No such file or directory

zsh running ls missing.txt > /dev/null, which still prints ls: missing.txt: No such file or directory.

The error still appeared. A command writes errors to a second stream, standard error, and > on its own redirects only standard output, file descriptor 1. bash printed the same in both steps.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting > /dev/null to hide errorsThey still print, as ls: missing.txt: No such file or directory didAdd 2> /dev/null for errors, or > /dev/null 2>&1 for both
Using it where you need the output laterWhatever goes to /dev/null is goneRedirect to a file instead, such as > output.txt

Troubleshooting​

SymptomLikely causeFix
A script prints messages you don't needIts commands write to the terminalAdd > /dev/null to those lines

FAQ​

How do I hide only the errors?​

Redirect standard error, file descriptor 2, with 2>: ls missing.txt 2> /dev/null printed nothing.

How do I hide both the output and the errors?​

ls notes.txt missing.txt > /dev/null 2>&1 printed nothing at all, although one file exists and the other doesn't. > /dev/null sends the output away, and 2>&1 then sends the errors to the same place.

Conclusion​

ls > /dev/null ran the listing and printed nothing, while ls missing.txt > /dev/null still showed its error. Use > /dev/null for output you don't need, 2> /dev/null for errors, and > /dev/null 2>&1 for both.

Additional Resources​