Bash printf control characters

Johnny Rosenberg gurus.knugum at gmail.com
Sat May 24 17:14:19 UTC 2014


I wrote this bash function, just for no particular reason, learning I guess:

function lvdati () {
    while [[ 1 ]]; do
        printf '\r%(%F %T (%A))T' -1
        sleep 0.1
    done
}

It displays the current date and time in the terminal (use Ctrl+c to
terminate it) in a certain format. On my machine, it looks like this, for
instance:
2014-05-24 18:58:50 (lördag)

What I don't want to happen is this:
2014-05-22 23:59:59 (torsdag)
Then a second later:
2014-05-23 00:00:00 (fredag))

Since ”fredag” (friday) is one character shorter than ”torsdag” (thursday),
the last character of the torsdag line will still remain, won't it?
So I need something at the end of the string that works like Ctrl+k in the
terminal (erase everything on the right side of the cursor), but how do I
implement it?

Let's say that Ctrl+k='\k' (it isn't, but just imagine it…), then I would
try:
function lvdati () {
    while [[ 1 ]]; do
        printf '\r%(%F %T (%A))T\k' -1
        sleep 0.1
    done
}

Suggestions (except adding one or more spaces to the string)?


Johnny Rosenberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20140524/5ecfc106/attachment.html>


More information about the ubuntu-users mailing list