-1
mint@mint-VirtualBox:/$ ls -l $(which cp)
-rwxr-xr-x 1 root root 153976 Sep  5  2019 /usr/bin/cp
mint@mint-VirtualBox:/$ which cp | ls -l
total 1043228
lrwxrwxrwx   1 root root          7 Jan 21 15:09 bin -> usr/bin
drwxr-xr-x   4 root root       4096 Jan 21 15:22 boot
drwxr-xr-x   2 root root       4096 Jan 21 15:10 cdrom
drwxr-xr-x  18 root root       4100 Jan 22 20:33 dev
drwxr-xr-x 149 root root      12288 Jan 21 15:22 etc
drwxr-xr-x   3 root root       4096 Jan 21 15:11 home
lrwxrwxrwx   1 root root          7 Jan 21 15:09 lib -> usr/lib
lrwxrwxrwx   1 root root          9 Jan 21 15:09 lib32 -> usr/lib32
lrwxrwxrwx   1 root root          9 Jan 21 15:09 lib64 -> usr/lib64
lrwxrwxrwx   1 root root         10 Jan 21 15:09 libx32 -> usr/libx32
drwx------   2 root root      16384 Jan 21 15:09 lost+found
drwxr-xr-x   2 root root       4096 Jan  3 18:00 media
drwxr-xr-x   2 root root       4096 Jan  3 18:00 mnt
drwxr-xr-x   2 root root       4096 Jan  3 18:00 opt
dr-xr-xr-x 162 root root          0 Jan 22 20:33 proc
drwx------   3 root root       4096 Jan 21 22:21 root
drwxr-xr-x  35 root root        960 Jan 22 20:38 run
lrwxrwxrwx   1 root root          8 Jan 21 15:09 sbin -> usr/sbin
drwxr-xr-x   2 root root       4096 Jan  3 18:00 srv
-rw-------   1 root root 1068185600 Jan 21 15:09 swapfile
dr-xr-xr-x  13 root root          0 Jan 22 20:33 sys
drwxrwxrwt  15 root root       4096 Jan 22 20:38 tmp
drwxr-xr-x  14 root root       4096 Jan  3 18:00 usr
drwxr-xr-x  11 root root       4096 Jan  3 18:34 var
mint@mint-VirtualBox:/$

Why do this two commands produce a different result? Thought the pipeline operator connects the output of one command with the input of a second one and since the output of which cp is the path of cp it should work or shouldn't it?

Thx for your time in advance, greetings!

2 Answers2

1
which cp | ls

tries to feed the standard output of which cp to the standard input of ls. But ls POSIX specification reads:

STDIN
Not used

So you can't pipe anything into ls. On the other hand, in

ls "$(which cp)"

the output of which cp is given as an argument to ls, which of course accepts arguments (by the way, quoting command substitutions as above is good practice).

Quasímodo
  • 2,104
1

As explained in Quasímodo's answer ls doesn't operate on stdin but you can force it to do so:

$ which cp | xargs  ls -l
-rwxr-xr-x 1 root root 153976 Sep  5  2019 /usr/bin/cp

See more info on xargs by typing man xargs.

And see Why not use “which”? What to use then?