Bash – Process substitution with input redirection

bashfile-descriptorsio-redirectionprocess-substitution

I am trying to understand input redirection in combination with process substituation. I am using bash 3

An example with tr is the following

$ tr "o" "a" <(echo "Foo")
tr: extra operand `/dev/fd/63'
Try `tr --help' for more information.

I think I understand why this does not work. The process substitution <( ) creates a file descriptor, where tr only reads from standard input.

How can I make it work with proper redirection?

I know that I could simply use pipes:

$ echo "Foo" | tr "o" "a"
Faa

However, I am trying to get a better understanding. I tried some thing with the help of man bash, by using <&, but I don't really know what I am doing.

How can I use process substitution properly using tr?

Best Answer

You were really close:

tr "o" "a" < <(echo "Foo")

The substitution <() makes a file descriptor and just pastes the path to the shell. For comprehension just execute:

<(echo blubb)

You will see the error:

-bash: /dev/fd/63: Permission denied

That's why it just pastes /dev/fd/63 into the shell and /dev/fd/63 is not excutable, because it's a simple pipe. In the tr-example above, it's echo "Foo" that writes to the pipe and via input redirection < it's the tr command that reads from the file descriptor.