Show persistent status message on console Show persistent status message on console shell shell

Show persistent status message on console


It might be worth having a look into the capabilities of tput.

Something like the following could form the beginning of a solution to always print the status line at the bottom of the screen:

numlines=$(tput lines)numcols=$(tput cols)numcols=$(expr $numcols - 1)separator_line=$(for i in $(seq 0 $numcols);do printf "%s" "-";done;printf "\n")tput cup $numlinesecho $separator_lineecho <your status line>

The intention of this logic is to:

  • work out how many lines on the screen and move to the bottom

  • work out how many columns and build the separator line to span that many columns

  • print the separator line and then your status line

Having said that, I feel certain there must be a more elegant way to achieve what you want to do...


While it would require having a client-side dependency, there is a way to do just this with screen.

#!/bin/bash# Check if script was started in our screen sessionif [ -z "$INTERNAL_INIT_SCRIPT" ]; then  # Create temporary screen config file to avoid conflicts with  # user's .screenrc  screencfg=$(mktemp)  # Show status line at bottom of terminal  echo hardstatus alwayslastline > "$screencfg"  # Start script in a new screen session  INTERNAL_INIT_SCRIPT=1 screen -mq -c "$screencfg" bash -c "$0"  # Store screen return code  ret=$?  # Remove temporary screen config file  rm "$screencfg"  # Exit with the same return code that screen exits with  exit $retfitotal=134function set_status {  screen -X hardstatus string "[$1/$total] $2"}# Prints "[1/134] Installing jq..." to the status lineset_status 1 'Installing jq...'# Install some component

I'm using this in a script I made to initialize Fedora servers I spin up on DigitalOcean. Here's what it looks like in practice:

Screenshot

Again, while it does involve installing screen, it is a commonly available tool, and using it for this purpose is exceedingly easy.


As Lindsay Winkler's answer suggests, tput is likely to do what you want if your terminal (emulator) supports it.

The capabilities available to tput are defined by terminfo, listed in the terminfo(5) man page.

Something like this should work (if anything does):

#!/bin/bashif tput hs ; then    tput tsl    echo -n This is the status line    tput fslelse    echo Sorry, there is no status linefi

(It prints the error message for me.)

You can use tput to print text on the bottom line, but then normal output will just overwrite it (unless you carefully control what's written to the screen).

The GNU screen command also makes some use of a status line, possibly emulating one if the terminal doesn't support it. man screen for details. But screen tends to use it for its own messages if it's available, and you don't seem to want to impose requirements on your users.