String multiplication in zsh

August 10, 2009

programming

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="------------------------------------"
Perl