Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
By the way, there's a difference between the print command the printf command. –  Flimm Nov 20 '13 at 9:05

6 Answers 6

up vote 10 down vote accepted

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.

share|improve this answer
    
Thanks man alot!! –  Narwen Nov 15 '13 at 16:44
    
@Narwen I sugest you to use the man all the time when you have to play with special characters. To search in man, type /your_string_to_search and then press enter. –  Radu Rădeanu Nov 15 '13 at 16:48
4  
The whole point of printf is that % character and what it does! –  Kaz Nov 15 '13 at 22:31

% is a special character in printf. That's what's causing the error. You need to escape it as %%.

$ will also be substituted so you'll need to escape that (\$) or use single quotes.

share|improve this answer
4  
The dollar sign is only special when it is followed by a valid variable name which is not the case here. –  glenn jackman Nov 15 '13 at 16:29
    
$variable is replaced by the shell, not by printf. If you surround your string with single quotes instead of double quotes, $ will no be interpolated by the shell and will get passed directly to printf. –  Flimm Nov 20 '13 at 9:01

It is a bit surprising to see that when you are not allowed to use the echo command that you chose to use the printf command instead.

Why not cat ?

#!/bin/bash

cat<<'EOF'
Different characters can be represented and supported
in the print command, for example:
x-y, X+Y, –, +, <, >, %, $, #, &.
EOF
share|improve this answer
    
But I had to replace echo with print command according to question. But thanks for the help. –  Narwen Nov 16 '13 at 4:27
1  
To avoid interpreting strings like $foo in the text, use cat <<'EOF' instead of cat <<EOF. –  Lekensteyn Nov 17 '13 at 10:01
    
@Lekensteyn yes indeed, thanks –  thom Nov 17 '13 at 13:29

the % is special for printf: it is the leading char in format specifiers. If you want a literal percent, use %%

share|improve this answer

printf is a formatted print which takes a format string as its first parameter. The rest of the parameters are used in the format string. The three things to remember when using printf are:

  1. Although many languages have printf statements/commands/functions, they all differ a bit so always verify details in language specific documentation (C, bash, php, perl, etc.),

  2. You should never use user supplied information in the format string. This is always a security flaw. If you want to display a user supplied string use a format string like "%s".

  3. Both the backslash (\) and the percent sign (%) are escape characters which change the meaning of the following character(s) if you want an escape character to appear in your output, it must be escaped or pulled in from another parameter. The dollar sign ($) is not special to printf, but is special to the shell, so be careful of your quotes.

printf is great for formatting numbers. look at the man pages for details

share|improve this answer

As others have mentioned, the % symbol is special for printf.

If you just want to print out a string as it is, give to printf %s as a first argument, and the string surrounded in single quotes as the second argument:

printf %s '
Different characters can be represented and supported 
in the printf command, for example: 
x-y, X+Y, –, +, <, >, %, $, #, &.
'

Using single quotes instead of double quotes stops the shell from interpolating symbols like $.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.