In a Bash script, I pass a command with arguments. This works fine, except when the command includes a redirection. In that case, the redirection character is treated as an ordinary character.
$ cat foo
#!/bin/bash
f() {
echo "command: $@"
$@
}
f echo a-one a-two
f 'echo b-one b-two'
f 'echo c-one c-two > c.tmp'
# I don't want to do f echo d-one d-two > d.tmp because I want to redirect the
# output of the passed command, not the output of the f() function.
$ ./foo
command: echo a-one a-two
a-one a-two
command: echo b-one b-two
b-one b-two
command: echo c-one c-two > c.tmp
c-one c-two > c.tmp
As you see, this prints “c-one c-two > c.tmp” when I wanted to print “c-one c-two” to file c.tmp. Is there a way to do that?