Copiando a seção relevante da resposta de Gilles :
How do I store a command in a variable?
“Command” can mean three things: a command name (the name as an executable, with or without full path, or the name of a function, builtin or alias), a command name with arguments, or a piece of shell code. There are accordingly different ways of storing them in a variable.
If you have a command name, just store it and use the variable with double quotes as usual.
command_path="$1" … "$command_path" --option --message="hello world"
If you have a command with arguments, the problem is the same as with a list of file names above: this is a list of strings, not a string. You can't just stuff the arguments into a single string with spaces in between, because if you do that you can't tell the difference between spaces that are part of arguments and spaces that separate arguments. If your shell has arrays, you can use them.
cmd=(/path/to/executable --option --message="hello world" --) cmd=("${cmd[@]}" "$file1" "$file2") "${cmd[@]}"
What if you're using a shell without arrays? You can still use the positional parameters, if you don't mind modifying them.
set -- /path/to/executable --option --message="hello world" -- set -- "$@" "$file1" "$file2" "$@"
What if you need to store a complex shell command, e.g. with redirections, pipes, etc.? Or if you don't want to modify the positional parameters? Then you can build a string containing the command, and use the
eval
builtin.code='/path/to/executable --option --message="hello world" -- /path/to/file1 | grep "interesting stuff"' eval "$code"
Eu sugiro que qualquer pessoa leia toda a resposta de Gilles . Ele contém muitas informações úteis.