Saving commands in variables is usually a bad idea (see BashFAQ # 050 for details). The reason it doesn't work, as you expect, is to ignore quoting values ββinside variables (unless you run it through something like eval , which then leads to other parsing).
In your case, I see three fairly simple ways to do this. First, you can use an alias instead of a variable:
alias TIME_CMD='/usr/bin/time -f "%E execution time"' TIME_CMD ls
Secondly, you can use the function:
TIME_CMD() { /usr/bin/time -f "%E execution time" " $@ "; } TIME_CMD ls
Third, you can use an array, not just a variable:
TIME_CMD=(/usr/bin/time -f "%E execution time") "${TIME_CMD[@]}" ls
Note that with an array you need to expand it using the "${array[@]}" idiom to properly preserve word breaks.
source share