3

Is it possible to insert the result of a command (or even a chain of commands) into an executable in one command? Something like this:

./a.out < echo 0

Or is it necessary to do this:

echo 0 > input.txt
./a.out < input.txt
Thomas Dickey
  • 76,765

2 Answers2

3

When you want the program a.out to read the output of the command echo 0 as its input, then you can do that like this:

echo 0 | ./a.out

Or (this is bash specific):

./a.out < <(echo 0)

This > and this < instead are redirection operators, > is the redirection of the output and < of the input.

This:

echo 0 > input.txt

Redirects the output of echo to a file called input.txt


This:

./a.out < input.txt

Redirects the input of ./a.out; the source are the contents of input.txt.


For multiple commands, use compound commands:

{ echo 0; cat test.txt; } | ./a.out
chaos
  • 48,171
0

Just to note that there is a simpler code (in bash):

./a.out <<<"0"

Or even:

 <<<"0" ./a.out

If you do not mind the "reverse looking" writing.