1

I know that we can use a file as stdin like

program << file.txt

but is it possible to use a string as stdin ?

4
  • 5
    Are you accidentally answering your own question here? That's awesome. Commented Nov 16, 2011 at 12:26
  • 1
    Ha, I didn't notice that before I posted my answer Commented Nov 16, 2011 at 12:27
  • @Ernest Friedman-Hill No I was not knowing that :) . I tried like program >> my stinrg but that was not working Commented Nov 16, 2011 at 12:36
  • The example in your question is invalid. file.txt is not used as input in that case but as the string marking the end of a here doc. Commented Nov 16, 2011 at 13:40

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

Like this:

echo "My string" | program

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.