Displaying Text with echo
The primary use of the echo
command is to display text or messages on the command line. This section will cover various techniques for displaying text using the echo
command.
Basic Text Output
The most basic usage of the echo
command is to display a simple string of text. For example:
echo "Hello, LabEx!"
This will output the message "Hello, LabEx!" to the console.
Displaying Variables
You can also use the echo
command to display the value of a variable. To do this, simply include the variable name within the string you want to display. For example:
NAME="John Doe"
echo "My name is $NAME"
This will output the message "My name is John Doe".
Displaying Multiline Text
If you want to display text across multiple lines, you can use the newline character \n
within the string. For example:
echo -e "Line 1\nLine 2\nLine 3"
This will output:
Line 1
Line 2
Line 3
Note the use of the -e
option to enable the interpretation of the \n
escape sequence.
Displaying Tabular Data
You can use the echo
command to display tabular data by incorporating tab characters (\t
) into the output. For example:
echo -e "Name\tAge\tCity"
echo -e "John\t30\tNew York"
echo -e "Jane\t25\tLos Angeles"
This will output a table-like structure:
Name Age City
John 30 New York
Jane 25 Los Angeles
By understanding these techniques for displaying text with the echo
command, you can effectively use it in your Linux shell scripts and command-line workflows.