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
.



Relevant Pages

  • Re: Writing simple IF statement
    ... It's mentioned in POSIX but undefined ... It's supported in all modern shells, ... So you don't consider dash a modern shell. ... programming language like perl/ruby/python, not a shell. ...
    (comp.unix.shell)
  • Re: Writing simple IF statement
    ... It's mentioned in POSIX but undefined ... It's supported in all modern shells, ... So you don't consider dash a modern shell. ... quite useful (though there are work-arounds for the POSIX syntax ...
    (comp.unix.shell)
  • Re: PS1 value in shells
    ... > Is the Bourne Shell actively developed? ... > features for scripting like modern shells ... but for scripting purposes it has a few key benefits. ...
    (comp.unix.shell)
  • Re: Unix-ify File Names
    ... special characters do make it more difficult to handle arbitrary file ... At least in POSIX shell. ... it's old value inside of the loop. ...
    (Debian-User)
  • Re: PHP [win32] & CLI Testers needed.
    ... written, lets just say to create shell emulation for now, and have ... My previous attempts to create the source below, ended with the loop ... finally resolve any issues due to this problem in PHP compatibility. ... Debug Warning: testforLinuxUser.php line 24 - stream_select: supplied argument is not a valid stream resource ...
    (php.general)