I'm writing a test script and it has a lot of repetition in it, and I'm looking for ways to reduce these code duplication. So I thought it would be nice to programmatically create the redirect part of the commands.
Here is a minimal example: (In the real script I would generate different output file names in x())
#!/bin/bash
set -x
x() {
echo '> out.txt 2> err.txt'
}
./someProgram $(x)
My hope would be that stdout would end up in out.txt and stderr in err.txt.
But bash quotes the string. So the resulting command is ./someProgram '>' out.txt '2>' err.txt instead of ./someProgram > out.txt 2> err.txt.
Here is the output of the example:
++ x
++ echo '> out.txt 2> err.txt'
+ ./someProgram '>' out.txt '2>' err.txt
.....
So is there a possibility that '>' and '2>' won't get quoted?
exec > out.txt 2> err.txtsomeProgram $(x)you can do it like this:x someProgram, i.e. thexfunction accepts an executable along with optional arguments and redirects its stdout and stderr as you wish