views:

78

answers:

2

In perl you can simply write print "-" x 20 and you get a line with dashes...but i need the same thing in bash/commandline on linux without perl/(g)awk etc. any ideas? The intention is to use it in the -exec of the find command and i want to prevent using simple echo "---------" ...

+2  A: 

you can create you own function

customprint(){
  num=$1
  symbol=$2
  for((i=1;i<=$num;i++));do printf "%s" $symbol; done
}

customprint 20 "-"

or

customprint(){
  num=$1
  symbol=$2
  a=$(printf "%0${num}s")
  echo ${a// /$symbol}
}

customprint 20 "-"

or

num=20
eval printf '%.0s-' {1..$num}
ghostdog74
+1, good one...
codaddict
Shouldn't that be `echo ${a//0/$symbol}`?
Alex Poole
no, "%0<num>s" gives you blanks. only if you use "%0<num>d" will it give you leading 0's
ghostdog74
The problem with own function is that i have to define it somewhere.
khmarbaise
you can define it in your shell script. or a library and you source it when you want to use.
ghostdog74
Interesting, `%0<num>s` gives me zeros on Mac OS X bash, Solaris bash/ksh, and HP-UX ksh; but spaces on Mac ksh. Don't have a Linux box handy though. Fun. `% ${num}s` seems to consistently give spaces though.
Alex Poole
A: 

So i have a little shorter solution

The following will produce a line with 60 dashes...without a function...

echo ={1..60} | sed  "s/[0-9]* \?//g"
khmarbaise
in your question, you said without using Perl/awk, so now you are using sed ?
ghostdog74
this is shorter, without using external tools: `eval printf '%.0s-' {1..$num}`
ghostdog74
Yeah you're right. Your suggestions is cooler than mine. Thanks.
khmarbaise