Artificial intelligent assistant

Passing a command with arguments and redirection to a function 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?

`eval "$@"` would do the job:


#!/bin/bash

f() {
echo "command: $@"
eval "$@"
}


but in general it's very unsafe to use it even though you'll see it here and there.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy dac8015e59bb9bfaaed9367d84211970