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?

closed as primarily opinion-based by Thomas Dickey, John, Jakuje, Jeff Schaller, PersianGulf Mar 11 '16 at 21:18

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

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 ?
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.

  • 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.