2

how can I use printf to print a row of minus symbols?

when I try: printf "-----------\\n"

I get:

bash: printf: - : invalid option
printf: usage: printf [-v var] format [arguments]

when I try: printf "\-\-\-\-\-\-\-\-\-\-\-\\n"

I get: \-\-\-\-\-\-\-\-\-\-\-

nath
  • 5,694

1 Answers1

2

It is an extremely in-efficient way to use printf() without using format specifiers. You generally define them to let know what type of output is being formatted. It should have been written as

printf '%s\n' "-----------"

Such that the printf matches the ----------- as a string type with the format specifier that takes a string keyword (%s). The \n after the specifier means, add the new line after the string is printed.

With the attempt what you have, when the quote removal happens printf interprets the dashes as one of its command line flags, which it does not understand.

Another hacky way of doing it would be to let printf know that its command line arguments are complete and interpret the content following it as its arguments. Most of the shell built-ins and/or external commands support this by suffixing a -- after the command keyword, i.e. as

printf -- "-----------\n"
Inian
  • 12,807
  • inefficient way of using printf ? so printing something like '---- 1 Jan 2020 ----'+CR have to be formated like this : printf '%s %s %s\n' '---' "$( date )" '---' ? i was guessing it is more printf '--- %s ---\n' "$( date )" like in other shell or C like format of printf – NeronLeVelu Jan 13 '20 at 11:33