4

I was advised to use command < <(command) instead of command <<< $(command) without further explanation. It works, but I don't understand it. Could someone please elaborate on this?

In the latter, I think (command) in brackets executes command in a subshell. The stdout is catched with $(). Finally <<< redirects it as a 'here file' to the stdin of the command in the parent shell.

In contrast the former construct redirects a file's content with the first < to the parent shell's stdin. The brackets execute the (command) in a subshell. So ´<(command)´ will save the subshell's stdout as a file, or how does this work?

1
  • 2
    I've written an answer about difference between shell redirections before askubuntu.com/q/678915/295286 , so to follow "don't repeat yourself" principle I'll just leave a link here. bash on Linux implements both <<< and < <() are implemented in very much different fashion below the hood (one is seekable and the other is not). Whether or not the person who suggested the change understands it is a different story. We also don't know what commands you actually use to see if the suggestion is valid. If anyone wants this as an actual answer - let me know, but I'll stop here. Jul 5, 2020 at 22:40

1 Answer 1

5

<<< is a here strings. Similar to here documents: The word after <<< and a newline are passed to the standard input of a command. Syntax: command <<< "some sentence" (Like echo "some sentence" | command, but without the overhead of the subshell)


Command Substitution: "$(cmd "foo bar")" causes the command 'cmd' to be executed with the argument 'foo bar' and "$(..)" will be replaced by the output. See http://mywiki.wooledge.org/BashFAQ/002 and http://mywiki.wooledge.org/CommandSubstitution


Process Substitution: <(command) or >(command) is replaced by a FIFO or /dev/fd/* entry. Basically shorthand for setting up a named pipe. See http://mywiki.wooledge.org/ProcessSubstitution Example: diff -u <(sort file1) <(sort file2)


command < <(othercommand) # Same thing as othercommand | command but without subshelling 'command'. Uses file redirection (<) to redirect a file created by process substitution (<()). The space between < and <(..) is important to avoid ambiguity (is it a heredoc, is it a redirected PS?).

3
  • So I understand < <(...) feeds a substituted process's output to a command to the left by writing it to a temporary FIFO and the reading it, all without the overhead of subshelling.
    – jamacoe
    Jul 5, 2020 at 21:48
  • Kinda correct but not exactly: /dev/fd/* is not necessarily FIFO itself - it's a file descriptor entry, and pairs of file descriptors are passed between subprocesses in pipelines (which is what <() does underneath the hood, it's really series of pipelines). As for subshelling, I'm not sure where that comes from as bash 4.4 manual that I have says absolutely nothing in the section about process substitution. You may want to revise or research that a bit more maybe. In fact try this: cat < <(echo $BASH_SUBSHELL) Jul 5, 2020 at 22:46
  • It's written FIFO OR /dev/fd/* Jul 5, 2020 at 22:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .