It is not necessary to surround the strings with quotes, as it does not affect what is written on the screen. If quotes (either single or double) are used, they are not repeated on the screen.
Fortunately, echo can do more than merely repeat verbatim what follows it. That is, it can also show the value of a particular variable if the name of the variable is preceded directly (i.e., with no intervening spaces) by the dollar character ($), which tells the shell to substitute the value of the variable for its name.
For example, a variable named
x
can be created and its value set to 5 with the following command:
echo, by default, follows any output with a
newline character
. This is a
non-printing
(i.e., invisible) character that represents the end of one line of text and the start of the next. It is represented by
\n
in Unix-like operating systems. The result is that the subsequent command prompt begins on a new line rather than on the same line as the output returned by echo.
The
-e
option is used to enable echo's interpretation of additional instances of the newline character as well as the interpretation of other special characters, such as a horizontal tab, which is represented by
\t
. Thus, for example, the following would produce a formatted output:
(The above command should be written on a single line, although it may render as two lines on smaller display screens.) The
-n
option can be used to stop echo from adding the newline to output.
By making use of output redirection, echo provides a very simple way of creating a new file that contains text. This is accomplished by typing
echo
followed by the desired text, the output redirection operator (which is a rightward pointing angle bracket) and finally the name of the new file. The file can likewise be formatted by using special characters. Thus, for example, the formatted output from the above example could be used to create a new file called
project1
:
echo can likewise be a convenient way of appending text to the end of a file by using it together with the the
append operator
, which is represented by two consecutive rightward pointing angle brackets. However, there is always the risk of accidentally using a single bracket instead of two, thereby overwriting all of the contents of the file, and thus, this feature is best reserved for use in scripts.
echo can also be used with
pattern matching
, such as the
wildcard character
, which is represented by the star character. For example, the following would return the phrase
The gif files are
followed by the names of all the .gif image files in the current directory:
echo is also commonly used to have a shell script display a message or instructions, such as
Please enter Y or N
in an interactive session with users.
echo is turned off automatically when passwords are entered so that they will not be shown on the screen.