Bash Echo Output
The echo command prints text to the screen, one line at a time. Small flags and quotes change exactly how that text comes out.
Several words, one line
Echo prints all four words on one line. One space appears between each pair of words.
echo Hello brave new worldJoining text on one line
The -n flag keeps the line open after each print. Three echo commands build a single shared line.
echo -n "Part one "
echo -n "part two "
echo "part three"Quotes keep wide gaps
The quoted line keeps its wide gaps in the output. The unquoted line below it collapses to single spaces.
echo "keep these gaps"
echo drop these gapsExample
echo "Server status report"
echo -n "Status: "
echo "all systems normal"
echo "Checked by hand"Key points
- echo prints each of its words with one space between them.
- A flag is a short option, starting with a dash, that changes a command.
- echo -n prints text without ending the line.
- Double quotes group words into one piece of text.
- Spaces inside double quotes print exactly as typed.
- Spaces outside quotes collapse into a single space.
In this example, the first echo prints the report title. The second echo uses -n, so its label stays on an open line. The third echo finishes that same line with the status value. The last echo prints a quoted line with its wide gaps kept.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does echo -n do differently from plain echo?