bashshellecho

How can I repeat a character in Bash?


How could I do this with echo?

perl -E 'say "=" x 100'

Solution

  • You can use:

    printf '=%.0s' {1..100}
    

    How this works:

    Bash expands {1..100} so the command becomes:

    printf '=%.0s' 1 2 3 4 ... 100
    

    I've set printf's format to =%.0s which means that it will always print a single = no matter what argument it is given. Therefore it prints 100 =s.

    NB: To print 100 dashes you need to escape the format string

    printf -- '-%0.s' {1..100}
    

    so that the dash is not interpreted as an option.