Output to stdout only when stdin has finished

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 ?

Answers


The simplest way would be

{
 echo "foo";
 sleep 1;
 echo "bar";
} > /dev/shm/file.$$; cat /dev/shm/file.$$

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.


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


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"
)"

Need Your Help

using custom CCK fields in node + Drupal 6 for some reason not accessible by $node ->field_name

drupal-6 cck

I have created a custom CCK field and gave proper permission to users, but