How can I repeat the character -
n times in shell? I have read and tried this, but this does not work for -
. It throws error invalid option
You can also use tput
along with printf
to accomplish filling the terminal with an exact number of dashes, either drawing the line directly, or writing the line of dashes to a variable (say line
) for repeated use, e.g. to write a screen width line of dashes to the variable line
, you could do:
$ eval printf -v line '%.0s-' {1..$(tput cols)}
and then simply echo "$line"
each time you need to draw the line. You can also write it directly to the screen with.
$ eval printf '%.0s-' {1..$(tput cols)}
(I'm not a huge fan of eval
, but this is one use where you are guaranteed the result of the command cannot be harmful).