How to repeat a dash (hyphen) in shell

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

    This throws an error:

    $ printf '-%.0s' {1..100}; echo ""
    bash: printf: -%: invalid option
    printf: usage: printf [-v var] format [arguments]
    

    This works fine under bash:

    $ printf -- '-%.0s' {1..100}; echo ""
    ----------------------------------------------------------------------------------------------------
    

    For other shells, try:

    printf -- '-%.0s' $(seq 100); echo ""
    

    The problem was the printf expects that - starts an option. As is common among Unix/POSIX utilities in this type of situation, -- signals to printf to expect no more options.

提交回复
热议问题