Confusion with printf command?
I have to print the following three lines in one print command without using echo command. So I have chosen printf command.Here are the three lines:
Different characters can be represented and supported
in the print command, for example:
x-y, X+Y, –, +, <, >, %, $, #, &.
What I have done so far is:
printf "
Different characters can be represented and supported
in the print command, for example:
x-y, X+Y, –, +, <, >, %, $, #, &.
"
But I got bash error for the third line ','.
So will anyone enlighten me up.
%
is a special character in printf
. That's what's causing the error. You need to escape it as %%
.
$
may also be substituted when within double quotes by the outer shell, so you should to escape that (\$
). It is usually just easier to use single quotes.
Better to use:
printf "Different characters can be represented and supported\n\
in the print command, for example:\n\
x-y, X+Y, –, +, <, >, %%, $, #, &.\n"
You get that error, as others have said here, because of %
character which is special and must to be escaped.
See man 1 printf
for more info.
the % is special for printf: it is the leading char in format specifiers. If you want a literal percent, use %%