Tail program output to stdout or stderr I have a piece of bash code that works fine.
for thing in &quot;$things&quot;; do
    output=$(my_program &quot;$thing&quot; 2&gt;&amp;1)

    if [[ $? -eq 0 ]]; then
        echo &quot;$(echo &quot;$output&quot; | tail -1)&quot;
    else
        echo &quot;$(echo &quot;$output&quot; | tail -1)&quot; 1&gt;&amp;2
    fi
done

The idea is to echo the last line of the output from the subcommand to stdout if it worked and to stderr if it didn't work.
Is there a better way to achieve this?
Don't quote $things:
for i in &quot;1 2 3 4&quot;; do
   echo &quot;i=$i&quot;
done
i=1 2 3 4
# Better is
for i in 1 2 3 4; do
   echo &quot;i=$i&quot;
done
i=1
i=2
i=3
i=4

When my_program is written well, errors are written to stderr and normal output to stdout. Your bash code is working well, so the program already uses the right returncode.
Can you change the my_program? When you can add an option -s (silent/summary), the program can make sure only 1 line is written to stdout (when OK) or stderr (when NOK):
for thing in ${things}; do
    my_program -s &quot;${thing}&quot;
done

Or without for-loop
printf &quot;%s\n&quot; ${things} | xargs -L1 -I{} my_program -s &quot;{}&quot;

When you can't change my_program, or you don't want to, move the special handling to a function and remove the additional echo commands:
tail_my_program() {
   output=$(my_program $* 2&gt;&amp;1)
   if (( $? == 0 )); then
      tail -1 &lt;&lt;&lt; &quot;${output}&quot;
   else
      tail -1 &lt;&lt;&lt; &quot;${output}&quot; &gt;&amp;2
   fi
}

for thing in ${things}; do
    tail_my_program &quot;${thing}&quot;
done

Your original solution and my function have a small bug: When my_program writes something to stdout after writing an error message to stderr, the stdout message is selected after an exit 1. This might never happen.