/dev/null
/dev/null is like a magic trash chute that swallows anything you drop in without a trace. You send noisy messages there when you do not want to see them.
In this page:
Syntax
command > /dev/null
command 2> /dev/null
/dev/null
/dev/null is a special file that discards anything written to it, commonly used to silence output you don't care about. Redirecting both streams there, command > /dev/null 2>&1, suppresses all output entirely.
Reading from /dev/null always immediately returns end-of-file with no data.
Note:
command > /dev/null 2>&1 is the standard idiom for "run this and hide all its output".Example: /dev/null
#!/bin/bash
echo "This will be discarded" > /dev/null
echo "This still prints normally"
ls /no/such/path > /dev/null 2>&1
echo "The error above was silenced. Exit code: $?"
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Redirecting with
> /dev/nulland still seeing errors, since you also need2>&1or2> /dev/null. - Writing
2>&1 > /dev/nullin the wrong order, so errors still appear on the terminal. - Hiding all output of a command and then being unable to find out why it fails.
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: