From: Janis Papanagnou on 17 Jun 2010 12:48 John Kelly wrote: > 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" If you're going to tweak that few lines of code you can as well write printf -v ts '%*s' $tn ''; echo "${ts// /${tc}}" (or preferable use printf as well, in both places, instead of echo). Janis > > >
From: Janis Papanagnou on 17 Jun 2010 12:51 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
From: John Kelly on 17 Jun 2010 12:55 On Thu, 17 Jun 2010 18:48:05 +0200, Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote: >> tc='='; tn=38 >> printf -v ts '%*s' $tn ''; ts="${ts// /${tc}}"; echo "$ts" > >If you're going to tweak that few lines of code you can as well write > > printf -v ts '%*s' $tn ''; echo "${ts// /${tc}}" > >(or preferable use printf as well, in both places, instead of echo). True. I kept the echo separate, to show how to get it in a variable. Perhaps you want to craft a function that uses ! variable indirection to return a variable, without printing it out. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: John Kelly on 17 Jun 2010 13:07 On Thu, 17 Jun 2010 18:51:25 +0200, Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote: >But consider that the $(...) variant will run on other modern shells as well >while the printf -v doesn't. I don't know about ksh, but bash spawns a subshell/pid for $(...) command substitution. The printf -v does not. >YMMV, but I consider a solution that runs on any modern shell to be preferable >to one that uses bash'isms unnecessarily. I tend to favor efficiency over portability. Linux and bash are so prevalent, they're nearly a de facto standard. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: Janis Papanagnou on 17 Jun 2010 13:29
John Kelly wrote: > > [...] Linux and bash are so > prevalent, they're nearly a de facto standard. Bash is "standard" on Linux, but not on commercial Unix'es, where ksh is prevalent. My Linux boxes have original ksh as well; which is what I use, actually. And, BTW, by "modern shells" I include zsh as well (which also doesn't seem to support that printf -v bash'ism that was suggested upthread). Janis |