3

This link is relevant What's the difference between substitution and piping to bash but I am not quite understanding everything that is being said.

What is the difference between piping command1 | command2 versus expanding commands command2 $(command1)? For example

vi $(find /home | grep xyzzy)

spits results out to vi to edit whereas

find /home | grep xyzzy | vi

doesn't seem to work for me. But I'm not understanding the fundamental difference.

Edit

Other relevant posts

Process substitution and pipe

Process substitution and pipe

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
RhythmInk
  • 511

2 Answers2

4
A|B

executes A and B (in parallel), and the standard output of A is fed into the standard input of B. In the case of

A $(B)

the shell executes first B, collects the standard output of B, then executes A, but for this execution assigns the individual word's to the ARGV-vector of A (i.e. A can access these words by the usual argv mechanism known from C and other language).

Aside from the fact that in both cases two programs A and B are involved, I don't see anything similar between them.

user1934428
  • 707
  • 5
  • 15
  • 1
    In what shell are piped commands run in parallel? How could that even occur? B has to read A's output (as B's input), which, as far as I'm aware, means A needs to produce an output before B can read it. Additionally, the line that could produce similar behavior using command substitution would be: B $(A), not A $(B), which is the OPs question. – De Novo Jan 28 '19 at 19:24
  • 1
    @DeNovo : If you have pipe A|B, the usual implementation is to start both processes A and B, and feed the stdout of A to the stdin of B. Without this, a construct such as A|tail -f would not work. – user1934428 Jan 29 '19 at 10:09
0

The first expands to vi <list_of_filenames>.

The second directly pipes the filenames to vi. Presumably, vi won't work like this. You can certainly pipe text to vim, but that is interpreted as the text to process, not the filename itself. (You's have to use vim -. See man vim for more information.)

Sparhawk
  • 19,941
  • why not using xargs? – cregox Mar 27 '21 at 06:41
  • @cregox Because that's not the question? – Sparhawk Mar 27 '21 at 11:52
  • then you don't have to use vim, at very least! – cregox Mar 31 '21 at 03:29
  • @cregox I think the question is really about the differences between piping and command substitution. The find part is just a dummy command to produce example output, as a proof of concept. They don't necessarily want to pipe the output of find. – Sparhawk Mar 31 '21 at 04:11
  • no matter how i look at it, i still find it very misleading to point the issue in his instance might be with vi, rather than with the difference between piping and command expansion, specially when xargs is specifically made to deal with this. – cregox Mar 31 '21 at 04:31