Most commands that accept --foo
as an option also accept --
by itself as an "end of options, start of arguments" marker - so you could do:
printf -- "--no-color\n--format-doc\n--no-profile\n" >> ~/.rspec-test
But the more specific answer to your exact example is that the first argument to printf is a format specifier, and you're making things more difficult than necessary by not using printf for its formatting abilities. This would be a better way to do what you want:
printf "%s\n" --no-color --format-doc --no-profile >> ~/.rspec-test
That tells printf to take each argument it gets and print it, followed by a newline. Easier than repeating the \n
yourself, and it avoids the leading --
problem you were facing. And it removes the need to escape any %
signs that your strings might contain.
As for how to do multiple lines with echo
, you could use:
echo -ne "--no-color\n--format-doc\n--no-profile\n" >> ~/.rspec-test
Or, much more portably:
{ echo --no-color; echo --format-doc; echo --no-profile; } >> ~/.rspec-test
Or using cat along with a here-doc:
cat >>.rspec-test <<EOF
--no-color
--format-doc
--no-profile
EOF