How to repeat a dash (hyphen) in shell

前端 未结 7 802
北海茫月
北海茫月 2020-12-31 10:10

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

相关标签:
7条回答
  • 2020-12-31 10:51

    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).

    0 讨论(0)
提交回复
热议问题