Bash printf control characters

Patrick Asselman iceblink at seti.nl
Mon May 26 10:34:29 UTC 2014


You could tell printf to use a certain width for your %A variable. The 
variable is printed aligned to the right of its assigned space, and the 
space in front is filled with space characters.

example:
printf "%20s" söndag

(haven't tested it, but should work)


Patrick


On 2014-05-24 19:14, Johnny Rosenberg wrote:
> 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))Tk' -1
>         sleep 0.1
>     done
> }
>
> Suggestions (except adding one or more spaces to the string)?
>
> Johnny Rosenberg




More information about the ubuntu-users mailing list