Writing Output but Preserving Spacing
Problem
You want the output to preserve your spacing.
Solution
Enclose the string in quotes. The previous example, but with quotes added, will preserve our spacing.
$ echo "this was very widely spaced" this was very widely spaced $
or:
$ echo 'this was very widely spaced' this was very widely spaced $
Discussion
Since the words are enclosed in quotes, they form a single argument to the echo command. That argument is a string and the shell doesn’t need to interfere with the contents of the string. In fact, by using the single quotes ('') the shell is told explicitly not to interfere with the string at all. If you use double quotes (“), some shell substitutions will take place (variable and tilde expansions and command substitutions), but since we have none in this example, the shell has nothing to change. When in doubt, use the single quotes.
See Also
help echo
help printf
Chapter 5 for more information about substitution
“echo Options and Escape Sequences” in Appendix A
Get bash Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.