3

When writing a bash script that contains message for the user, I can write

echo Processing files...

or

echo "Processing files..."

In this and many other cases, the output will be the same. However, is it good practice to routinely put all echo strings in double quotes? What are pros and cons? What do expert bash scripters do?

I understand that

echo This    is     my      sentence.

and

echo "This    is     my      sentence."

will produce different outputs (single space between words in first case, spacing retained in second case), but are there other reasons to regularly use double quotes by default? Or not to?

EDIT: I realize that single quotes have a different effect; this question is specifically about double quotes.

EDIT: To prevent this just being a matter of opinion, let me rephrase a little:

  1. What is the practical effect (and pitfalls, if any) of routinely putting all echo commands in double quotes?

  2. What are the pitfalls of NOT putting most echo strings in double quotes?

3

There are lots of reasons, but the main ones:

  • better control over the inclusion of whitespace (which you noticed)
  • partial protection against unexpected shell expansion, e.g., of glob characters * and ?
| improve this answer | |
0

Although I'm voting to close because the answers to this question are primarily (almost purely) opinion-based, there is an important difference between double-quotes and single-quotes here. The double quotes will interpolate variables inside the quoted string (as will not quoting at all), single quotes will not do so.

| improve this answer | |
  • agreed, but OP asked about double-quotes, which do have their place in scripting. I marked it to close as well, but this is one of those cut/paste magnets... – Thomas Dickey Mar 11 '16 at 19:10

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