How can I execution a bash line(command) that contains the redirection characters? For instance, the following line can't be execution.
home>CMD="ls -l > out"
home>$CMD
ls: cannot access >: No such file or directory
ls: cannot access out: No such file or directory
Thanks for your help in advance.
eval $CMD
将做您想要的。
You could use eval, but it is not recommended. If the purpose of storing the command in a variable is to execute it multiple times, it is better to use a function.
Example:
dols() {
ls -l > out
}
dols
See also: I'm trying to put a command in a variable, but the complex cases always fail!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.