Reputation: 8981
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.
Upvotes: 11
Views: 1558
Reputation: 50002
The dev-null-cli package abstracts this away for you in a cross-platform way. Add it to your devDependencies, and then you can write
echo Hello | dev-null
Upvotes: 0
Reputation: 148
The best solution to this question depends on the environment. You will have to depend on something to do this.
In the original question, execution from Python or Java was mentioned. Using a C program is mentioned in the answer by prushik and Remi Guan. I, myself, got to this question looking for an answer to use in a Makefile (on Windows).
All solutions work on Linux and Windows. They discard any output to stdout. Output to stderr will still be shown.
Beware that the Python, Java and C solutions all use piping, which discards the exit code. Retrieving the exit code is again OS dependent 😢. See: Batch file equivalent of PIPESTATUS
The answers using Python, Java and C all use a program that does nothing. You don't need to actively discard the piped input, just doing nothing with it is enough 🙂
I think the Python solution is the most elegant, followed by Java, and C is the least elegant. Except if you're already in a Makefile of course.
When you have python installed you can simply do:
echo Hello | python -c ""
Java is a bit more involved. You need a minimal java program source in your working directory.
nullpipe.java
class nullpipe {
public static void main (String args[])
{
}
}
Then you can use:
echo Hello | java nullpipe.java
The C solution follows the same principle as the Java solution, but this time you can't combine compilation and execution.
nullpipe.c
int main(){}
Before execution you need to compile.
gcc nullpipe.c -o nullpipe
Then you can execute:
echo Hello | nullpipe
You might want remove nullpipe
afterwards. The residual executable nullpipe
is my reason to call this the least elegant solution.
I was running make on Windows and thought I needed to use echo Hello > NUL
, as is normal in the Windows Command Shell. But as it turns out, make was executing my commands in a bash shell (although I started it from Windows Command Shell). So, both in Windows and Linux you can use.
echo Hello > /dev/null
Sidenote: My version of make was installed using Scoop. If your version of make behaves different, please let me know in the comments. I'll update my answer accordingly.
Upvotes: 0
Reputation: 331
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".
Upvotes: 2
Reputation: 86443
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.
Upvotes: 0