14

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?

3
  • 2
    It's called Process substitution
    – anishsane
    Apr 16, 2015 at 4:07
  • 1
    Thanks. so I looked at the link you mentioned. In that, the example for 'wc' uses a single <( ... ). So why is the extra "<" required in the mapfile case for taking in input? Apr 16, 2015 at 4:10
  • 2
    From the perspective of the process wc, the example uses wc filename syntax. <(...) is process substitution syntax. From logical perspective, <(...) gets expanded to a filename, & anything read from that file is the output of command between the parenthesis. You can check other examples on that page or try your own examples.
    – anishsane
    Apr 16, 2015 at 4:13

1 Answer 1

21

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

0

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.