excess.org

Ian Ward

Consulting
Boxkite Inc.
Software
CKAN contributor/tech lead
PyRF primary contributor
Urwid author
Speedometer author

Presentations
Contributing to Open Source
IASA E-Summit, 2014-05-16
Urwid Applications
2012-11-14
Urwid Intro
2012-01-22
Unfortunate Python
2011-12-19
Django 1.1
2009-05-16

Writing
Moving to Python 3
2011-02-17
Article Tags

Home

Ian Ward's email:
first name at this domain

wardi on OFTC, freenode and github

Locations of visitors to this page

watch(1) in bash with Unicode and Colour support

Posted on 2009-07-25.

watch(1) is a very useful little tool when you want to see the results of a command changing over time.

Unfortunately it seems that it doesn't support Unicode or colours in its output. This is a short bash function that does much of what watch can do, but with no trouble handling Unicode or coloured output.

botch() {
    while true; do
        (echo -en '\033[H'
            CMD="$@"
            bash -c "$CMD" | while read LINE; do 
                echo -n "$LINE"
                echo -e '\033[0K' 
            done
            echo -en '\033[J') | tac | tac 
        sleep 2 
    done
}

This bash function ("botch" because it rhymes with "watch") executes the command passed every two seconds and displays its output without clearing the screen, so as long as the output is not longer than the screen there will be no flicker. The \033[H escape sequence moves to home position (top-left), \033[0K erases to the end of the line and \033[J clears to the bottom of the screen. tac | tac buffers the output until the command is finished.

Examples:

botch ls --color

botch unicode '${RANDOM:1}'

Tags: Software Linux