Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat a dash (hyphen) in shell

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. Below is the exact command that I used:

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

Original posted line: printf '-%0.s' {1..100}

I also tried escaping - by putting a \ but in that case it repeats \- n times.

like image 355
Sonu Mishra Avatar asked Aug 09 '16 23:08

Sonu Mishra


1 Answers

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.

like image 127
John1024 Avatar answered Oct 15 '22 13:10

John1024