If the unnecessary output of your script bothers you, dump it to /dev/null.
While developing a script to automate a task, you might find that the output is printed even though it actually doesn’t require it at all, and because of this, your terminal becomes crowded with unneeded text.
The best way to solve this problem is to redirect your script’s output to /dev/null, which will effectively discard the output and prevent it from clogging up your terminal.
So let’s talk about how to redirect the output of a script to /dev/null and what the advantages and disadvantages are of redirecting output to /dev/null.
What is /dev/null/ in Linux, and how will it sink the output of your script?
The obvious question is: what exactly is /dev/null/, and why do we use it? /dev/null is a special file that is basically used in shell scripting to dump or discard output (stdout), errors (stderr), or both (stdout and stderr).
It is often called the “black hole” because data written there can never be retrieved.
It is important to note that you cannot execute /dev/null on your terminal, and because of that, you cannot use /dev/null in piping with another command.
If you don’t believe me, take a look at the output of stat /dev/null.
where you can see that /dev/null is a character device, that it doesn’t take up any space, so its size is 0 bytes, and its type is “character special” with no executable permission to run.
Then the obvious follow-up question is, “How exactly can I make use of /dev/null?” You can send the standard output or standard error of a command to /dev/null with the redirection operator and the right file descriptor.
I’m assuming you’re familiar with the concept of “redirection,” which allows you to save the results or failures of a command to a file rather than showing them on the screen. I’ll try to explain some of the concepts here so you can get a sense of how to use them with /dev/null, but if you’re unsure, read this article.
Redirecting output (stdout) to /dev/null in Linux
Let’s start with Stdout, which stands for “standard output” and is the default output channel of a command when the command successfully executes and produces some output.
This output can be directed to any file or even /dev/null if we do not need it. To explain how to redirect output to /dev/null, first see how it works.
I’ll start by showing you what happens if I don’t use /dev/null and then redirect the output to /dev/null. For example, if I want to print the files that are located in the /etc/ssh/ directory, then I can simply use:
$ ls -l /etc/ssh
And here is the output of the above command, which displays the file content of the /etc/ssh directory:
Now, I’ll use the redirection operator (>) with file descriptor “1” to hide the output from the last command.
To do this, I simply type the same command, but add “1>/dev/null” or else use the shorter syntax “> /dev/null” to the end.
$ ls -l /etc/ssh 1> /dev/null
$ ls -l /etc/ssh > /dev/null
Great! As you can see in the result below, I was able to use the redirection operator to hide the output that was first printed to the screen.
Now it is your turn to complete this task, and you should use the same command syntax I just demonstrated.
Redirecting error output (stderr) to /dev/null in Linux
Stderr, which is short for “standard error,” causes any warnings or errors caused by a command to be displayed on the screen, which is very useful in showing errors and warnings, but it can often be distracting from the output that one is looking for.
Using the redirection operator, you can redirect these errors and warnings to a text file or even completely away using /dev/null.
For example, if I list a directory that doesn’t exist, then of course an error message will get printed on the screen like “ls: cannot access ‘/etc/ssh/unknown’: No such file or directory.”
To suppress this error, I can use the redirection operator as follows:
$ ls /etc/ssh/unknown 2> /dev/null
The output of the above command is shown below:
That’s not hard to understand, is it?
Redirecting output and errors (stdout & stderr) to /dev/null in Linux
Depending on your needs, you can either suppress the error or output the message using the above method, but if you want neither to be displayed on the screen, send both standard output and errors to /dev/null, you need to follow the below steps.
For this purpose, you need to combine standard output (1) and standard errors (2) together using the “>” operator and redirect them to /dev/null, like this: command > /dev/null 2>&1
Let’s take an example. If you don’t want to print the error or success message of the command, you can use the following command to suppress both messages:
$ ls /etc/sshs/ > /dev/null 2>&1
Confused with the above command? The command given above can be hard to understand, but it isn’t too hard to figure out.
According to the availability of the file or directory “ls,” the command will throw an error or output, but it will not display it here because it will be sucked by /dev/null.
And then, 2>&1 will be sucking the output of /dev/null because this “/dev/null” is available on your file system, which can interfere, so 2>&1 will make sure not to print anything.
Instead of using the above command, you can use the one below, which is more simple and easy to use:
$ ls /etc/sshs/ &> /dev/null
See the results for yourself, with output from both commands showcased below.
And with that, I shall end.
Quick reference to redirect output to /dev/null
- Use the following command to direct standard (stdout) output messages to /dev/null:
- Use the following command to send standard (stderr) error messages to /dev/null:
- Use the following command to send both standard output and standard error to /dev/null:
This can be a great way to ensure that your scripts are running smoothly and efficiently, as the output will not clutter up your terminal
With this in mind, it’s important to remember that redirecting output to /dev/null has its uses, but if you’re not careful, it can cause problems because it throws away all output and makes it hard to debug.
Anyway, if there is something I need to consider, please use the space below to provide any additional information or feedback.
Till then, put your feet up and relax. As always, the next article will be up shortly.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.