Few days ago I was writing a ruby wrapper for SoX command line tool. To reduce disk IO I wanted to use process substitution. It’s a cool shell feature which allows to use command output as an input file for another command. It’s pretty useful if the second command doesn’t work with standard input or you need to pass more than 1 input.
Let me show the classic example(works in bash and zsh):
<(echo 'Saluton!') is treated like a file which contains line
Underhood bash(zsh) creates a named pipeline where output of
echo 'Saluton!' is written.
Then the named pipeline is passed to
You can see it:
So I wanted to use it in ruby:
But unfortunately it doesn’t work:
The problem is that ruby’s
system method and back quotes use
not your current shell (which in my case is
$0points to the current script or to interpreter if you’re running it interactively.
Fortunately there is a way to create a workaround to run bash:
1 2 3 4 5 6
Bash has option
-c which takes bash script to execute.
is a standard ruby library which provides a method to escape shell commands.
So now it works as we want it to be:
1 2 3
Thanks for reading!