On Windows, one can use > NUL
to redirect a pipe to nothing. On Linux, one uses > /dev/null
. Is there a cross-platform compatible way to redirect a pipe to nothing for both platforms? In other words, I would be able to use this and not worry about which platform the command is executing on.
For example, I would like to be able to write the following commands as one command.
echo Hello > NUL
echo Hello > /dev/null
Of course, anything more complicated would require separate scripts. The use case is a single command being executed from cross-platform language (e.g. Java, Python). I would like to avoid having to detect the platform and instead use a generic solution.
Furthermore, the use case is such that 100s of machines will be accessed and installing would be more complex than simply detecting the platform and adapting the command.
In Unix, how do I redirect error messages to /dev/null? You can send output to /dev/null, by using command >/dev/null syntax. However, this will not work when command will use the standard error (FD # 2). So you need to modify >/dev/null as follows to redirect both output and errors to /dev/null.
The /B option prevents start from opening a new terminal window if the program you are running is a console application. and NUL is Windows' equivalent of /dev/null .
After executing the ping command, '>/dev/null' tells the system to suppress the output, and '2>&1' directs the standard error stream to standard output. In this way, all output of the command is discarded.
/dev/null in Linux is a null device file. This will discard anything written to it, and will return EOF on reading. This is a command-line hack that acts as a vacuum, that sucks anything thrown to it.
Write a simple C program which accepts all input from stdio and just ignores it.
Name your program something like null
and put it in the PATH
for your operating system.
Then, you can do:
echo Hello | null
The program is simple to write. It is a little more than "hello world".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With