Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command which copies stdin to stdout

Tags:

windows

cmd

I'm updating the test suite of the standard library of the Crystal programming language to run on Windows. To test the spawning of new processes with pipes for stdout and stdin on unix, cat is used as a "dummy process" which copies stdin to stdout for testing.

What's the simplest command to achieve the same effect on windows using cmd.exe commands?

like image 678
Stephie Avatar asked Mar 28 '26 00:03

Stephie


2 Answers

A simple command that can be found in all windows versions that can be used as an alternative to the linux cat command in the indicated scenario is

find /v ""

As is, it will read lines from standard input and write them to standard output.

note: While more and findstr "^" could, sometimes, be used, both have several limitations that make find /v "" a better alternative.

like image 167
MC ND Avatar answered Mar 31 '26 03:03

MC ND


Here is my solution:

powershell.exe -c "[Console]::OpenStandardInput().CopyTo([Console]::OpenStandardOutput())"

The accepted solution of using (find /v "") has some problems:

  • It corrupts binary data. It tries to read the input as text with CRLF line endings. If you pass it data which doesn't contain a newline, it will output a CRLF at the end. It also fails if there is a line longer than its line limit (4091 characters)
  • When given empty input, it returns an exit code of 1. By contrast, Unix cat will return an exit code of 0 when the input is empty. A test which tests the exit code and expects it to be zero will fail when the input is empty. A workaround is to do find /v "" & : (assuming you are running it from cmd.exe, such as by using cmd.exe /c) – but then, if the find actually fails to run for any reason (see next bullet point), you will not get a failure exit code to notify you
  • On some systems, you may have MSYS/Cygwin/etc find in your path before Windows find. (Of course, in that case you probably have cat in your path too, and hence don't need to do this at all – but, you may be looking for a solution which you know will work even on systems without MSYS/Cygwin/etc installed.) Assuming you are running it using cmd.exe /c, you can solve this by using %windir%\system32\find.exe /v ""

I think this PowerShell oneliner is better in that it avoids the above problems.

like image 26
Simon Kissane Avatar answered Mar 31 '26 05:03

Simon Kissane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!