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

/dev/null

In this page:

  1. /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: $?"

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.