I often encounter /dev/null in bash scripts but never know what it’s for. There are also many variations > like 2>/dev/null or &> /dev/null or > /dev/null 2>&1 and so on.
/dev/null is a method of suppressing output. 1 denotes stdout, while 2 denotes stderr. Essentially, &> /dev/null is a new syntax for 2>&1.
Here are the different options for dev/null:
ls 1> /dev/null will
not show the result in terminal
ls 2> /dev/null will
show the result in terminal
ls &> /dev/null
will not show the result in terminal
ls -0 1> /dev/null
will show the error in terminal
ls -0 2> /dev/null
will not show the error in terminal
ls -0 &> /dev/null
will not show the error in terminal
Check out this example:
ping -c 1 danduran.me &> /dev/null; echo $?
will print out 0 if the ping succeeded.
Enjoy!
Dan D.