28

I want to print double quotes using echo statement in shell programming.

Example:

echo "$1,$2,$3,$4";

prints xyz,123,abc,pqrs

How to print "xyz","123","abc","pqrs";

I had tried to place double quotes in echo statement but its not being printed.

Nagaraju
  • 1,853
  • 2
  • 27
  • 46

3 Answers3

45

You just have to quote them:

echo "\"$1\",\"$2\",\"$3\",\"$4\""

As noted here:

Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’. The characters ‘$’ and ‘`’ retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘"’, ‘\’, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ‘!’ appearing in double quotes is escaped using a backslash. The backslash preceding the ‘!’ is not removed.

The special parameters ‘*’ and ‘@’ have special meaning when in double quotes (see Shell Parameter Expansion).

Xavi
  • 20,111
  • 14
  • 72
  • 63
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • 1
    It should be `printf` not `print` – Ashish Gaur Sep 21 '13 at 05:17
  • You probably meant to say `echo` instead of `print`! – devnull Sep 21 '13 at 05:18
  • 1
    Yes `print` command is used instead of `echo` in `ksh` but the question is asked for `bash`, you should edit your answer as well or atleast specify the `ksh` limitation – Ashish Gaur Sep 21 '13 at 05:22
  • @snyder Changed the obvious. I'm not sure how that would make a difference since the topic's solution is generally about quoting. – konsolebox Sep 21 '13 at 05:25
  • 1
    @konsolebox did you read the comment by Raju? that's the difference it will make and also the future readers will try your command and will get the same error. – Ashish Gaur Sep 21 '13 at 05:29
  • Can you suggest me a online source for shell programming@konsolebox @synder – Nagaraju Sep 21 '13 at 05:39
  • 1
    @Raju I always prefer referring the [Advanced Bash-Scripting Guide](http://www.tldp.org/LDP/abs/html/) for starters. It seems updated as well. – konsolebox Sep 21 '13 at 06:36
  • @konsolebox The ABS is problematic; I like to refer newcomers to http://wiki.bash-hackers.org/scripting/tutoriallist instead – tripleee Apr 13 '16 at 04:40
16

Use printf, no escaping is required:

printf '"%s","%s","%s","%s";\n' "$1" "$2" "$3" "$4"

and the trailing ; gets printed too!

konsolebox
  • 72,135
  • 12
  • 99
  • 105
devnull
  • 118,548
  • 33
  • 236
  • 227
  • Only that printf is not compatible to all shells. – konsolebox Sep 21 '13 at 05:21
  • 1
    Hmm.. `printf` is defined by [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html). – devnull Sep 21 '13 at 05:23
  • Yeah, but not in System V. – konsolebox Sep 21 '13 at 05:24
  • 4
    The fact that System V is not POSIX-compliant in no way detracts from this answer. Besides, the question is tagged `bash`, so the operating system should be irrelevant. – chepner Sep 21 '13 at 17:17
  • @chepner Sometimes we can't really assert that a thread is specific to `bash` just because of the tag since there are many reasons why it could have been added there. Some just tag it implying that bash is shell itself while some just place it for more audience. Sometimes relying on the title could be more accurate. – konsolebox Sep 21 '13 at 17:44
  • Maybe explain that the trailing `;` gets printed because it's also inside the quotes, not specifically because you used `printf`. – tripleee Apr 13 '16 at 05:35
  • if $1, $2, $3 or $4 has spaces your command will not do the job – Maksym Ganenko Oct 20 '18 at 10:59
4

You should escape the " to make it visible in the output, you can do this :

echo \""$1"\",\""$2"\",\""$3"\",\""$4"\"
Ashish Gaur
  • 2,030
  • 2
  • 18
  • 32
  • The first version runs into issues if any of the positional parameters contains a `%` character; the second works. – Jonathan Leffler Sep 21 '13 at 06:15
  • Your argument should also be placed around a single pair of doublequotes: `echo ""\"$1\",\"$2\",\"$3\",\"$4\""` or else variables would be subject to word splitting with IFS and possible pathname expansion. – konsolebox Sep 21 '13 at 17:34