Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash syntax mapfile input redirection

Could someone explain me this syntax for mapfile input redirection?

mapfile -t array < <(./inner.sh)

(from this StackOverflow answer by gniourf_gniourf)

As I understand, the first "<" is to take input from the output of whatever is on the right side of it. But what is the <( ... ) syntax? Why is the second "<" required?

like image 460
Sujay Phadke Avatar asked Apr 16 '15 04:04

Sujay Phadke


1 Answers

The reason the linked example looks like this:

wc <(cat /usr/share/dict/linux.words)

And the mapfile answer looks like this:

mapfile -t array < <(./inner.sh)

Is because of the difference in the wc and mapfile commands and how the substituted process needs to be given to command.

As anishsane says the expansion of <(command) is a filename and can be used anywhere a filename can be used.

wc takes filenames as arguments so it can be used directly as an argument.

mapfile reads from standard input so to get it to read from a specific file you use you redirect standard input from the file </path/to/somefile but as I just said the expansion of <(command) is a filename so you can use that in the input redirection.

However, you cannot just concat the two bits directly (the way you can with a file name/path) because << is also a valid shell construct (a here document) and that would be ambiguous to the shell. So to avoid that you need to space out the two < characters and end up with < <(command) (which is analogous to < file which is perfectly legal).

like image 168
Etan Reisner Avatar answered Oct 23 '22 10:10

Etan Reisner