0

My aim is to echo a character, for example #, based on a value such as num=6 and it must print # 6 times on the screen.

Not sure how to get this.

StackUseR
  • 884
  • 1
  • 11
  • 40
Christopher Karsten
  • 387
  • 1
  • 2
  • 12
  • What did you try? There are plenty of similar questions available that you look around? – Inian Nov 25 '16 at 12:34
  • 1
    Possible duplicate of [Print a character repeatedly in bash](http://stackoverflow.com/questions/5799303/print-a-character-repeatedly-in-bash) – StackUseR Nov 25 '16 at 12:35
  • 1
    Possible duplicate of [How can I repeat a character in bash?](http://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash) – fuglede Nov 25 '16 at 12:36
  • Possible duplicate of [ksh scripting, For loop](http://stackoverflow.com/questions/9977485/ksh-scripting-for-loop) – ceving Nov 25 '16 at 15:28

2 Answers2

1

You could do something like

 printf '#%.0s' {1..6}

or, in the more general case,

 printf '#%.0s' $(seq 1 $num)
fuglede
  • 17,388
  • 2
  • 54
  • 99
  • @ceving which [tag:ksh]? It works in the canonical AT&T ksh `Version AJM 93u+ 2012-08-01` – Henk Langeveld Nov 27 '16 at 13:57
  • @HenkLangeveld It might work in the version you have quoted, but the major reason to use the KornShell is to write scripts, which work also on older Unix systems. If you use a ksh syntax enriched with Bash features, you will give up the portability benefit, which make is almost useless to use the KornShell at all. The `ksh` of Solaris 10 does not support `{1..6}`. – ceving Nov 28 '16 at 09:21
  • Ah yes, you're correct there. Ksh93 was not included before Solaris 11 (not counting dtksh). For older unixen it will be safer to stay with pure POSIX shell. – Henk Langeveld Nov 28 '16 at 22:45
0
printf "%*s" "$num" " " | tr " " "#"

or

yes '#' | head -"$num" | tr -d "\n"
Walter A
  • 19,067
  • 2
  • 23
  • 43