the function run does not see > and dump.sql. That is probably not what you want because it prevents you from changing all the redirections with a single environment variable, as the output of echo "${@}" is redirected to the file then, too. Thus, you should use something like run --redirect dump.sql mysqldump blabla, see below.
Stick with
"$@"and useeval. This may take you to a quoting nightmare, of course. You have to quote everything except for the>so that the shell sees a bare>in the command line before it does quote removal.Handle the redirection seperatelyseparately:
run --redirect dump.sql mysqldump blabla run() { if [ "$1" == '--redirect' ]; then shift redirect_target="$1" shift else redirect_target='/dev/stdout' # works at least with Linux fi if [[ "$(printenv DRY_RUN)" = "yes" ]] then echo "${@}" else "${@}" > "$redirect_target" fi }