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

1
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.

2
  • 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. 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.)

5
  • why not using xargs?
    – cregox
    Mar 27 at 6:41
  • @cregox Because that's not the question?
    – Sparhawk
    Mar 27 at 11:52
  • then you don't have to use vim, at very least! 😁
    – cregox
    Mar 31 at 3: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 at 4: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 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.