TIL /dev/null
/dev/null
is a special file present on every Linux system. You can only write to /dev/null
, not read.
Some people call it the null device.
Anything you write to /dev/null
is discarded and disappears.
It’s like a vacuum into nothingness.
Why would you need such a device?
The Purpose of the Null Device is to Silence an Output
/dev/null
is very handy when you want to ‘silence’ an output.
You can redirect stdout
or stderr
(or both) to the null device (/dev/null
) to silence any output.
To do so, you need to :
-
redirect
stderr
(2) tostdout
(1):2>&1
-
redirect both to
/dev/null
:/dev/null 2>&1
The whole command becomes:
[my-command] > /dev/null 2>&1
Our Use case
Our case is very simple: we use Nx to manage our frontend pipeline locally - but our build server are running on FreeBSD.
Long story short, some dependencies issues do not allow us to fully use Nx
on our build servers.
Therefore, for our production builds we use a Makefile
pipeline - which makes silencing webpack
’s output … Challenging.
/dev/null
allows us to easily silence the output on our build server to reduce the noise on our logs.
Coupled with environmental variables, we can easily create a ‘forced silent’ mode on our servers when needed.
Conclusion
The null device is a tool useful when you need to silence an output, whether it is coming from the standard output or the standard error.
By the way, it is commonly used in bash scripting, so you will come across /dev/null 2>&1
every now and then in scripts.
Remember, all it does is silencing the output.
See you soon 👋,
Also