Re: Output n equal signs from a script



pk wrote:
Janis Papanagnou wrote:

pk wrote:
Marc Muehlfeld wrote:

Hello,

how can I print n equal signs in a shell script, without using a for
loop, like

for i in `seq 1 20` ; do
echo -n "="
done

Is there a better way and just with bash build-ins?
With bash and builtins:

printf -v string "%30s" " "
echo "${string// /=}"

This fails for the corner case n=0; which produces 1 (instead of 0)
characters. Instead use

printf -v string "%30s" ""

True, thanks.

Also, it's easy to be portable; use

string=$( printf "%30s" "" )

Modern shells (I think ksh optimizes such types of calls) may not even
use a subshell for that expression.

Well yes, but that's only one part of it; the other key element is
${string// /=} which is also nonstandard; given this, and also that the OP
explicitly mentioned bash, I thought I could just as well use other
bashisms.

But consider that the $(...) variant will run on other modern shells as well
while the printf -v doesn't.

YMMV, but I consider a solution that runs on any modern shell to be preferable
to one that uses bash'isms unnecessarily.

Janis
.