9

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.

Nathan
  • 8,093
  • 8
  • 50
  • 76
  • 2
    `if (windows) { > NUL } else { > /dev/null }` – Robert Harvey Aug 21 '14 at 23:32
  • 8
    I can't see how this can be much useful... It's not like this is the only difference between `sh` and `cmd.exe`, you'll have to write separated scripts anyway for anything that goes a tiny bit farther "launch this command". – Matteo Italia Aug 21 '14 at 23:44
  • You could write a shell function / doskey macro that redirects appropriately, then do something like `tonull echo Hello`. Matteo's point's solid though - this is hard to use for anything realistic. You can get cygwin and run shell scripts on Windows. – Tony Delroy Aug 22 '14 at 01:27
  • 4
    @MatteoItalia This is the last thing I need to make my NPM script completely cross-platform (to suppress some loud logging). So yes this can be useful. – Stijn de Witt Jan 03 '16 at 06:22
  • I think it is a good question, but unfortunately for us it seems the answer is there is no such cross-platform way ... :( – Stijn de Witt Jan 08 '16 at 23:39

2 Answers2

2

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

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
prushik
  • 322
  • 2
  • 7
  • Good idea. The particular use case I am facing would require installing such a program on a lot of machines. – Nathan Aug 22 '14 at 14:38
  • Great answer, cause this gave me the idea to redirect to `cd` like `echo hello | cd`. It will just print the current prompt, no command output, no errors, results in success exit code, and no need to install or write anything. Works in Windows and Linux – Slav Oct 07 '22 at 17:27
0

If you are only interested in running a command and (possibly) its return code, why not have the calling/parent application receive and just discard any input? It would generally involve setting up a pipe or stream (e.g. for Java) and possibly a separate thread, but it should not be too complex.

It might help if you mentioned which command you are launching and why. Some commands on Unix-like systems, for example, have a special command line option that suppresses most of the output.

Community
  • 1
  • 1
thkala
  • 84,049
  • 23
  • 157
  • 201