I have an executable that is used in a way such as the following:
executable -v -i inputFile.txt -o outputFile.eps
In order to be more efficient, I want to use a Bash variable in place of the input file. So, I want to do something like the following:
executable -v -i ["${inputData}"] -o outputFile.eps
Here, the square brackets represent some clever code.
Do you know of some trick that would allow me to pipe information into the described executable in this way?
Many thanks for your assistance
You can use the construct
<(command)
to have bash create a fifo with commands output for you. So just try:
-i <(echo "$inputData")
Echo is not safe to use for arbitrary input.
To correctly handle pathological cases like inputdata='\ntest'
or inputdata='-e'
, you need
executable -v -i <(cat <<< "$inputData")
In zsh
, the cat
is not necessary
Edit: even this adds a trailing newline. To output the exact variable contents byte-by-byte, you need
executable -v -i <(printf "%s" "$inputData")
Note: zsh only:
To get a filename containing the contents of ${variable}
, use:
<(<<<${variable})
Note:
<<<${variable}
redirects STDIN
to come from ${variable}
<<<${variable}
is equivalent to (but faster than) cat <<<${variable}
So for the OP's case:
executable -v -i <(<<<${inputData}) -o outputFile.eps
Assuming that your executable file name is exec and defined variable name is x and you want to pass the value of x as input to the executable. Then do this:
$ echo $x > stdin && ./exec < stdin
this will do the job
executable -v -i <<<"${inputData}" -o outputFile.eps
will do the trick in bash.