Writing Output to the Terminal/Window
Problem
You want some simple output from your shell commands.
Solution
Use the echo built-in command. All the parameters on the command line are printed to the screen. For example:
echo Please wait.
produces
Please wait.
as we see in this simple session where we typed the command at the bash prompt (the $ character):
$ echo Please wait. Please wait. $
Discussion
The echo command is one of the most simple of all bash commands. It prints the arguments of the command line to the screen. But there are a few points to keep in mind. First, the shell is parsing the arguments on the echo command line (like it does for every other command line). This means that it does all its substitutions, wildcard matching, and other things before handing the arguments off to the echo command. Second, since they are parsed as arguments, the spacing between arguments is ignored. For example:
$ echo this was very widely spaced this was very widely spaced $
Normally the fact that the shell is very forgiving about white space between arguments is a helpful feature. Here, with echo, it’s a bit disconcerting.
See Also
help echo
help printf
“echo Options and Escape Sequences” in Appendix A
“printf” in Appendix A
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access