Step by step breakdown of /dev/null

Jorge Yau
Jorge Yau
Oct 25, 2016 · 3 min read
# First let's try writing to this file.
$ echo 'text' > /dev/null
# Upon inspection, we see that the write was successfully.
$ echo $?
0
http://man7.org/linux/man-pages/man1/ls.1.html

$ ls
Applications Documents Library Music Public
$ echo $?
0
$ ls -0
ls: illegal option -- 0
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]
$ echo $?
1
$ ls -0 > /dev/null 2>&1
$ echo $?
1
$ ls -0 > /tmp/devnull 2>&1
$ echo $?
1
$ cat /tmp/devnull
ls: illegal option -- 0
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

function cmd_exists() {
command -v $1 > /dev/null 2>&1
}
# cmd_exists ls; echo $?
# cmd_exists sl; echo $?

Jorge Yau

Written by

Jorge Yau

Web Engineer at Stash. I write about NYC, tech, and the immensity of life.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade