Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command output redirection using pipe character

I try to understand how output redirection to another command works.

I know that it can be used if the second command is find as help text to find explicitly says so. It can also be used with findstr although findstr's help text does not say anything about this.

So the question is:

How do I know what other windows command shell commands accept output from another command?

I've created a command line where I expect the output to be "18:33" but it does not work

C:>time /T | set /P a= && echo %a%
%a%

C:>setlocal enabledelayedexpansion
C:>time /T | set /P a= && echo !a!
!a!

Why does this not work as expected?

Please not that the point here is not reading the time to a variable. I just don't see how the pipe is useful with anything other than find or findstr.

like image 385
Dlanod Kcud Avatar asked Jun 05 '26 07:06

Dlanod Kcud


2 Answers

Nearly every command that accepts input can also be used with pipes.

Also your sample with SET /P could work, but it needs some attention.
I don't use delayed expasion here, as it adds only more complexity.

time /T | ( set /P var= && set var )

The parenthesis are important here, else the implicit order would be

( time /T | set /P var= ) && set var

But this fails, as a pipe creates two new cmd.exe instances for both sides.
After the end of the both tasks these instances will be removed again.
Therefore it's only possible to access a variable created by a pipe inside the same (new) instance.

For many cases, pipes are useful to automate some simple questions like

echo Y | del *

Some more infos about pipes and delayed expansion at SO:Why does delayed expansion fail when inside a piped block of code?

like image 99
jeb Avatar answered Jun 07 '26 10:06

jeb


The short answer is because windows is messed up.

There is no good reason why your commands above do not work, and microsoft is entirely to blame. Their software quite often not only fails to work, but it actually fails to work in completely nonsensical ways, and without any error message to hint at what might have gone wrong, thus wasting people's time. If you want a decent command prompt which usually works, and in the event that it does not work it is generally due to your mistake and not its mistake, try Linux.

That having been said, there are several commands available at the windows command prompt that do process their standard input; the sort command is one that comes to mind. Try type text.txt | sort and see what happens.

To answer your question "how do I know?", the answer is that you cannot know, because I do not think that there is a comprehensive list of all commands that process the standard input. However, generally, if a command operates on an input file, and it requires the input file name to be specified as a parameter, and if this parameter is optional, then it is pretty safe to assume that if the parameter is omitted, the command will process its standard input.

Also, it is possible to fool a command which accepts the input file name as a non-optional parameter to use the standard input instead, by specifying con as the input file name. So, for example, typing type alone does not work, but try type con and see what happens.

like image 30
Mike Nakis Avatar answered Jun 07 '26 10:06

Mike Nakis