janneb
janneb

Reputation: 37188

Redirect stdin in a script to another process

Say I have a bash script that get some input via stdin. Now in that script I want to launch another process and have that process get the same data via its stdin.


#!/bin/bash

echo STDIN | somecommand

Now the "echo STDIN" thing above is obviously bogus, the question is how to do that? I could use read to read each line from stdin, append it into a temp file, then

cat my_temp_file | somecommand

but that is somehow kludgy.

Upvotes: 14

Views: 6158

Answers (2)

javidcf
javidcf

Reputation: 59681

When you write a bash script, the standard input is automatically inherited by any command within it that tries to read it, so, for example, if you have a script myscript.sh containing:

#!/bin/bash

echo "this is my cat"
cat
echo "I'm done catting"

And you type:

$ myscript.sh < myfile

You obtain:

this is my cat
<... contents of my file...>
I'm done catting

Upvotes: 18

choroba
choroba

Reputation: 241758

Can tee help you?

echo 123 | (tee >( sed s/1/a/ ) >(sed s/3/c/) >/dev/null )

Upvotes: 2

Related Questions