bash & printf: minus sign at start of string: invalid option

Linux howto's, compile information, information on whatever we learned on working with linux, MACOs and - of course - Products of the big evil....
Post Reply
User avatar
^rooker
Site Admin
Posts: 1483
Joined: Fri Aug 29, 2003 8:39 pm

bash & printf: minus sign at start of string: invalid option

Post by ^rooker »

[PROBLEM]
When using printf in bash scripts, it might accidentially happen that someone starts the formatting string with a "-" (hyphen/minus) character.
Printf will interpret this as parameter and (a) either do something that wasn't intended, or (b) throw the following error message:
bash: printf: - : invalid option
printf: usage: printf [-v var] format [arguments]
[SOLUTION]
To solve this, the "-" sign has to be disarmed.
Add the string "--" as first argument before the format string and it should work.

Example:

Code: Select all

printf -- "- Name: %s" "$NAME"
Jumping out of an airplane is not a basic instinct. Neither is breathing underwater. But put the two together and you're traveling through space!
Post Reply