Issue
I am trying to print "-" multiple times using printf
. I am using the below command to print the same character multiple times, which works fine for all except "-".
printf "`printf '=%.0s' {1..30}` \n"
When I try to do the same for "-", it gives error.
printf "`printf '-%.0s' {1..30}` \n"
bash: printf: -%: invalid option
It is trying to take it as user-passed option. How do I work around this?
Solution
Pass --
before everything else to each printf
invocation:
printf -- "`printf -- '-%.0s' {1..30}` \n"
Like many commands, printf
takes options in the form of tokens starting with -
(although -v
and --
are the only options I know). This interferes with your argument string, as printf
is instead trying to parse -%.0s
as an option. For that case however, it supports the --
option (like many other commands), which terminates option parsing and passes through all following arguments literally.
Answered By - Siguza Answer Checked By - Gilberto Lyons (WPSolving Admin)