← Back to Bash Course | Chapter 3: Control Flow | Lesson 9 of 14

/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:

  1. /dev/null
Syntax
bash
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

bash
#!/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: $?"
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
  1. Redirecting with > /dev/null and still seeing errors, since you also need 2>&1 or 2> /dev/null.
  2. Writing 2>&1 > /dev/null in the wrong order, so errors still appear on the terminal.
  3. Hiding all output of a command and then being unable to find out why it fails.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.