Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a cross-platform compatible way to redirect to NUL or /dev/null?

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.

like image 270
Nathan Avatar asked Aug 21 '14 23:08

Nathan


People also ask

How do I redirect to Dev Null?

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.

Is there a Windows equivalent 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 .

What means 2 >/ 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.

What is Dev Null used for?

/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.


1 Answers

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".

like image 61
prushik Avatar answered Oct 04 '22 06:10

prushik