What is the difference between echo and/dev/null in command for clearing file content?
We know that there are many methods to clear the file content. This article only talks about the differences between echo "And/dev/null.
1. What is the black hole device/dev/null?
In Linux, the null device is basically used to discard output streams that are no longer needed by a process, or as a blank file of an input stream. These can usually be achieved through the redirection mechanism.
Therefore, the/dev/null device file is a special file. It will empty all input sent to it, and its output can be considered as an empty file.
In addition, you can use the cat command to display/dev/null and redirect the content to a file to clear the file. By using cat/dev/null, the file size is 0 bytes.
Ii. What are the main differences between the two?
Null strings are not equivalent to null. A string indicates that it is a specific thing, but its content may be empty, but null means that a thing does not exist.
For this reason, when you redirect the echo command output to a file as input and use the cat command to view the content of the file, you will see a blank line (an empty string ).
To use null as the output input to the file, you should use the-n option, which will tell echo not to output the new line ending with the command above.
Iii. Practical Operation
Use cat/dev/null. The file size is 0.
When echo "" is used, the file size is 1, because echo only writes an empty string to the file. If you want to set the file size to 0, use echo-n ""