How To Use Echo Command.
The echo command is one of the most basic and frequently used commands in Linux. The arguments passed to echo are printed to the standard output.
echo is commonly used in shell scripts to display a message or output the results of other commands.
echo is a shell builtin in Bash and most of the other popular shells like Zsh and Ksh. Its behavior is slightly different from shell to shell.
There is also a standalone /usr/bin/echo utility, but usually, the shell built-in version will take precedence. We will cover the Bash builtin version of echo.
The syntax for the echo command is as follows:
When the -n option is used, the trailing newline is suppressed.
If the -e option is given, the following backslash-escaped characters will be interpreted:
\\ – Displays a backslash character.
\a – Alert (BEL)
\b – Displays a backspace character.
\c – Suppress any further output
\e – Displays an escape character.
\f – Displays a form feed character.
\n – Displays a new line.
\r – Displays a carriage return.
\t – Displays a horizontal tab.
\v – Displays a vertical tab.
The -E option disables the interpretation of the escape characters. This is the default.
There are a few points to consider when using the echo command.
The shell will substitute all variables, wildcard matching, and special characters before passing the arguments to the echo command.
Although not necessary, it is a good programming practice to enclose the arguments passed to echo in double or single quotes.
When using single quotes ” the literal value of each character enclosed within the quotes will be preserved. Variables and commands will not be expanded.
The following examples show how to use the echo command:
Display a line of text on standard output.
Display a line of text containing a double quote.
To print a double quote, enclose it within single quotes or escape it with the backslash character.
Display a line of text containing a single quote.
To print a single quote, enclose it within double quotes or use the ANSI-C Quoting .
Display a message containing special characters.
Use the -e option to enable the interpretation of the escape characters.
Pattern matching characters.
The echo command can be used with pattern matching characters, such as the wildcard characters. For example, the command below will return the names of all the .php files in the current directory.
Redirect to a file
Instead of displaying the output on the screen, you can redirect it to a file using the >, >> operators.
If the file.txt doesn’t exist, the command will create it. When using > the file will be overwritten, while the >> will append the output to the file .
Use the cat command to view the content of the file:
echo can also display variables. In the following example, we’ll print the name of the currently logged in user:
$USER is a shell variable that holds your username.
Displaying output of a command
Use the $(command) expression to include the command output in the echo’s argument. The following command will display the current date :
Displaying in color
Use ANSI escape sequences to change the foreground and background colors or set text properties like underscore and bold.
By now, you should have a good understanding of how the echo command works.