Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何执行包含重定向字符的 bash 行(命令)?例如,以下行不能被执行。
home>CMD="ls -l > out" home>$CMD ls: cannot access >: No such file or directory ls: cannot access out: No such file or directory
提前感谢您的帮助。
eval $CMD会做你想做的。
eval $CMD
您可以使用 eval,但不建议这样做。如果将命令存储在变量中的目的是多次执行,则最好使用函数。
例子:
dols() { ls -l > out } dols
另请参阅:我正在尝试将命令放入变量中,但复杂的情况总是失败!