Escaping hyphens (--) with printf in bash

Solution 1:

To avoid an argument starting in - being interpreted as an option, you have a few different alternatives:

1) Move the minus signs form the format string to the argument string, possibly reflecting the new length of the printed string in the %.s format specifier. As in:

$ printf "%.60s\n" "---- $1 --------------"

2) bash's builtin commands accept -- as an option that instructs them to stop processing options and consider everything that follows as file-names and arguments. As in:

$ printf -- "---- %.55s\n" "$1 --------------"

3) You can substitute the minus signs with their octal codes. Actually, replacing just the first one works, as in:

$ printf "\055--- %.55s\n" "$1 --------------"