Reputation: 20554
What is the simplest command to wait for stdin to be finished to output something to stdout.
For example, If I write :
{
echo "foo"
sleep 1
echo "bar"
} | command
It should show
foo
bar
after 1 second (and not the first foo at the beginning).
My solution so far is to do twice tac
, For example :
{
echo "foo"
sleep 1
echo "bar"
} | tac | tac
It has the advantage of handling all ANSI escapes/colors well.
Is they any simpler way to do what I want ?
Upvotes: 0
Views: 179
Reputation:
Capture output, then echo/print it:
output=$(
echo "foo"
sleep 1
echo "bar"
)
printf "%s\n" "$output"
Or just:
printf "%s\n" "$(
echo "foo"
sleep 1
echo "bar"
)"
Upvotes: 0
Reputation: 3410
Alternatives using bashims:
cat <<EOF
$(
echo foo
sleep 3
echo bar
)
EOF
# or
cat <<<"$(echo foo;sleep 3;echo bar)"
Internally bash will create a safe temporary file, put the output of the commands into it, then pass it to cat
stdin.
The file is destroyed as soon as the command (here cat
) release it stdin
.
Note usually it is better to use a stream.
Upvotes: 1
Reputation: 69208
The simplest way would be
{
echo "foo";
sleep 1;
echo "bar";
} > /dev/shm/file.$$; cat /dev/shm/file.$$
Upvotes: 1
Reputation: 20554
One way to do this would be to use the printf command :
For example
{
echo "foo"
sleep 1
echo "bar"
} | printf "%s\n" "$(cat)"
Would do the trick
Upvotes: 0