Hardstatus in screen (& other terminals)

The incredibly useful tool screen has an option “hardstatus” that you might want to set to “on” and “lastline”. With these settings, the last line of your terminal gets used as status line for the applications in the screen.

The programms running in the screen can set the message of the status line to any text they want. While close to no program makes use of this, you can use it in your own shell scripts.

START_STATUS=$(echo -n -e '\x1b]0;')
END_STATUS=$(echo -n -e '\x7')
trap 'echo "${START_STATUS}${END_STATUS}"' EXIT

for pos in {1..10000}; do
    echo -n "${START_STATUS}Working on #$pos${END_STATUS}"
    longrunningcommand
done

The first two lines store the control codes to set the status line. (Don’t forget the end sequence!)

The third line clears the status line when the script exits (for whatever reason.)

Several long running tasks are started, updating the status line before each command.