From: Janis Papanagnou on 17 Jun 2010 12:12 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" "" 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. Janis
From: John Kelly on 17 Jun 2010 12:17 On Thu, 17 Jun 2010 18:04:14 +0200, Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote: >Ignoring the OP's wish for using only built-ins; we can as well ignore which >characters are used for padding... > n=20 > printf "%*s\n" $n "" | sed s/./=/g I didn't know you could use the * notation with bash printf. Seems to work though. tc='='; tn=38; printf -v ts "%*s" $tn ''; ts=${ts// /${tc}}; echo "$ts" ====================================== You can adapt that to make a function. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: John Kelly on 17 Jun 2010 12:27 On Thu, 17 Jun 2010 16:17:24 +0000, John Kelly <jak(a)isp2dial.com> wrote: >tc='='; tn=38; printf -v ts "%*s" $tn ''; ts=${ts// /${tc}}; echo "$ts" >====================================== > >You can adapt that to make a function. One more tweak. You can use single quotes around the format specifier, and the paramteter expansion that assigns ts to itself should use double quotes. tc='='; tn=38 printf -v ts '%*s' $tn ''; ts="${ts// /${tc}}"; echo "$ts" -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: pk on 17 Jun 2010 12:28 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.
From: John Kelly on 17 Jun 2010 12:47
On Thu, 17 Jun 2010 17:28:34 +0100, pk <pk(a)pk.invalid> wrote: >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. I guess we had the same idea about the same time. I did not see yours before posting mine. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php |