简体   繁体   English

执行包含重定向字符的bash行(命令)

[英]Executing bash line(command) that contains redirection characters

How can I execution a bash line(command) that contains the redirection characters? 如何执行包含重定向字符的bash行(命令)? 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. 您可以使用eval,但不建议这样做。 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! 另请参阅: 我试图将命令放入变量中,但是复杂的情况总是失败!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM