In complex shell scripts, it is useful to have more output control with echo. This is very handy in a function where output will be used as a return value, for example.

Here are some tips:

To suppress trailing newline, use -n option to echo:

$ echo -n "text without newline at the end"
text without newline at the end$

By default, echo will not interpret escape characters, but you can use -e option to enable this interpretation:

$ echo -e "this\tis\vstring\fwith\nescape\tcharacters"
this    is
          string
                with
escape  characters
$

There are more escape characters available, see man echo.

As usual, different string expansion is performed within different quotes (which is done by the shell).

Usually, there are two echo commands available: echo program (usually located at /bin/echo) and echo shell builtin (created for performance reasons). The behaviour of these two commands is almost the same, and you can ignore the differences (which can be seen in documentation for echo program (man echo) and for your shell (help echo)).

Leave a Reply

Close Menu