everyone, i found this strange problem ( maybe it is a feature ) this afternoon.
In my current working directory, there are 5 files, namely a,b,c,d,e
If i issue the following command
echo "a" | ls -l
What I expect is only the detailed information of file a is listed.
But from the output, the detailed information of file a,b,c,d,e are listed.
It seems as if the command before the pipe is not working, eg, the following command will output exactly as if only ls -l is executed.
echo "whatever_you_type" | ls -l
I am confused, can anybody help to explain this issue?
I know i can workaround this problem by using xargs, something like echo "a" | xargs ls -l
But I want to know why.
When you pipe something into another process, it will be pushed into its
STDINstream. As ls does not read from stdin, it just ignores that. What you want is that the parameters are passed to the command line ofls. This can be achieved by using something like this:Alternatively, you can use xargs which can be used to construct command line parameters from stdin input. In your case, you could use it like
It will execute an
ls -l [filename]for each space-separated string of stdin input.