Search code examples
linuxbashcommand-linepipels

Why "which cp | ls -l " is not treate as "ls -l $(which cp)"?


According to pipe methodology in Linux, the output of the first command should be treated as input for the second command. So when I am doing which cp | ls -l, it should be treated as ls -l $(which cp)

But the output is showing something else.

Why so ?


Solution

  • ls does not take input from stdin. You can work around this if you need to by using xargs:

    which cp | xargs ls -l
    

    This will invoke ls -l with the (possibly multiple, if which were to return more than one) filenames as command line arguments, with no standard input.