_      _            _
         _ __ ___ (_) ___| | __  _ __ | |
        | '_ ` _ \| |/ _ \ |/ / | '_ \| |
        | | | | | | |  __/   < _| | | | |
        |_| |_| |_| |\___|_|\_(_)_| |_|_|
                  | |
                  |_|       Thoughts on (technical) stuff...
You're from: 67.202.9.192

In Perl you have this:

% perl -e 'print "a" x 5, "\n"'
aaaaa

With that you can easily create a separator string consisting out of 60 spaces.

I always missed this in my shell - until now.

In Zsh have the following expansion:

l:expr::string1::string2:

Pad the resulting words on the left. Each word will be truncated if
required and placed in a field expr characters wide.

See zsh.dotsrc.org. There is also a r: variant which operates in the same way.

And lo and behold:

% echo ${(r:40::-:)A}
----------------------------------------

No more:

A="------------------------------------"
Posted in: perl, zsh

2 comments

Well Miek,

In zsh it's a nice, single command function.
In bash it takes two steps, something like

A="$(printf "%-20s", "")"
A="${A// /-}"

This is all shell internal, so performance should be good.

Like the zsh trick better, though.
Still a lot nicer than:
LINE="--------------------------"

I like it.

Comments are closed

If you really, really want to comment, please mail miek@miek.nl.

0 comments in moderator queue