Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for passing a single line of input to a command's stdin using bash

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?

like image 516
Pete Hodgson Avatar asked Feb 03 '26 15:02

Pete Hodgson


1 Answers

wc -w <<<"one two three"

is a shorter way.

like image 128
Michael Krelin - hacker Avatar answered Feb 05 '26 06:02

Michael Krelin - hacker