Vivek Goel
Vivek Goel

Reputation: 24150

Using a string as stdin in linux

I know that we can use a file as stdin like

program << file.txt

but is it possible to use a string as stdin ?

Upvotes: 1

Views: 2277

Answers (3)

sorpigal
sorpigal

Reputation: 26086

bash supports "here strings" with the following syntax:

program <<<"your input goes here"

This will be treated approximately the same as

echo "your input goes here" > tmp
program < tmp

Upvotes: 3

Didier Trosset
Didier Trosset

Reputation: 37437

Like this:

echo "My string" | program

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81694

You can use syntax called a here document:

program << EOF
input
more input
even more input
EOF

This is supported in several UNIX shells, as well as in some scripting languages like Perl.

Upvotes: 3

Related Questions