使用 bash 将单行输入传递到命令的标准输入的最佳实践
我有一个命令行程序,它从标准输入获取输入。对我来说,使用 bash 将一行标准输入传递到该程序中的最佳方法是什么?我有两种方法可以完成这项工作,但它们看起来都有点笨拙。
我将使用一个简单的计算三个单词的例子来说明我的方法。
使用此处的文档:
wc -w <<EOS
one two three
EOS
使用 echo:
echo 'one two three' | wc -w
正如我所说,这两个看起来都有点笨重。有没有更干净的方法来实现这一点?
I have a command-line program which takes input from stdin. What's the best way for me to pass a line of stdin into that program using bash? I have two approaches which accomplish the job, but they both seem a bit clunky.
I'll illustrate my approaches using a dumbed-down example of counting three words.
using a here doc:
wc -w <<EOS
one two three
EOS
using echo:
echo 'one two three' | wc -w
As I said, both of these seem a bit clunky. Is there a cleaner way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是一个更短的方法。
is a shorter way.
我见过最常用的
echo
。第三个选项可能仅适用于 Bash(我还没有检查)是“here-string”,
<<<
(您可以在
Bash 手册的 >Here Strings
部分)I've seen the
echo
one used most often.A third option, which might be Bash-only (I haven't checked) is the "here-string",
<<<
(you can find out more about those in the
Here Strings
section of the Bash manual)