如何使用重定向和管道制作bash命令字符串? [英] How can I make a bash string of command with redirect and pipe?
本文介绍了如何使用重定向和管道制作bash命令字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用重定向和/或管道制作一个bash命令字符串,并用它显示命令字符串或执行命令字符串.一个没有重定向或管道的简单命令有效,但是带有重定向或管道的一串命令无效.例如,
I want to make a bash string of commands with redirect and/or pipe, and use it to either display the string of commands or execute the string of commands. A simple command without redirect or pipe works, but a string of commands with redirect or pipe does not. For example,
command="echo 1"
$command
echo "$command"
将显示
1
echo 1
但是,
command="echo 1 | cat"
$command
echo "$command"
会显示
1 | cat
echo 1 | cat
但是,我想要
1
echo 1 | cat
类似地,用于重定向
command="echo 1 | cat > 1.out"
$command
echo "$command"
会显示
1 | cat > 1.out
echo 1 | cat > 1.out
但我想要
echo 1 | cat > 1.out
其中有一个名为"1.out"的新文件,其中包含内容1.
with a new file named "1.out" with content 1 in it.
有没有办法实现我想要的?
Is there a way to achieve what I want?
推荐答案
如果您希望外壳程序将字符串作为命令求值,请告诉它这样做:
If you want the shell to evaluate the string as a command, tell it to do so:
eval "$command"
echo "$command"
这篇关于如何使用重定向和管道制作bash命令字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文