How to repeat a dash (hyphen) in shell

前端 未结 7 869
北海茫月
北海茫月 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:46

    I would recommend using a traditional for loop, as there is no need to spawn sub-processes or expand 100 arguments:

    N=100
    for((i = 0; i < $N; ++i)); do
      printf -
    done
    

    It is curious that printf -%s triggers "invalid option" but printf - does not. To perhaps be extra safe, you could do printf %s -.

提交回复
热议问题