Process substitution in bash
- Published at
- Updated at
- Reading time
Today I came across this little snippet of shell code
bash <(curl -s https://codecov. I found it in the documentation of Codecov, which is a code coverage tool mainly run in CI.
This "command structure"
bash <( was entirely new to me. So I dug a little bit.
What you see there is called process substitution, and the definition of it is as follow:
Huh – that's interesting. So let's give this a try:
$ echo <(ls .) /dev/fd/11
Okay, this is a file name. Let's try something else and look into the file using
$ cat <(ls .) README.md assets ... ...
Interesting! Looking into this file path shows the files printed by the
How can you use process substitution then?
As you've seen,
<( lets you use the output of a command as a written file.
Process substitution is handy if you want to "pipe" something into a command that doesn't understand UNIX pipes. The codecov example shows that a bash script is downloaded from
https://codecov, the content is written to a file and
bash executes this file. Pretty cool, hmm?
In a Node.js context, you can use it like this:
$ node <(echo 'console.log("foo")') foo
But even more fantastic, you could use process substitution to execute stuff from your clipboard quickly. I'm on a Mac, which means I have the
$ node <(pbpaste)
In short: process substitution allows you to skip temporary files and write some fancy one-liners. Sweet! 🎉