The echo command in Unix and Linux is used to display a line of text or string to the standard output (usually the terminal). It is a simple but essential command that is frequently used in shell scripts and command-line operations to print messages, variables, or results of commands.
The syntax for the echo command is:
echo [option] [string...]To display a simple string of text:
echo "Hello, World!"This will output:
Hello, World!
To display the value of a variable, use the variable name preceded by a dollar sign ($):
NAME="Alice"
echo "Hello, $NAME!"This will output:
Hello, Alice!
You can concatenate multiple strings or variables:
FIRST="Alice"
LAST="Smith"
echo "Hello, $FIRST $LAST!"This will output:
Hello, Alice Smith!
The echo command supports several options that modify its behavior:
-n: Do not output the trailing newline.-e: Enable interpretation of backslash escapes.-E: Disable interpretation of backslash escapes (default).
The -n option prevents echo from adding a newline at the end of the output:
echo -n "Hello, World!"The output will be:
Hello, World!%
(Note: The % symbol here represents the prompt appearing immediately after the output.)
The -e option allows the interpretation of backslash escapes, which can be used to format the output:
\n: New line\t: Horizontal tab\b: Backspace\a: Alert (bell)\r: Carriage return\\: Backslash\c: Suppress further output
For example:
echo -e "Hello,\nWorld!"This will output:
Hello,
World!
Another example with tabs and backslashes:
echo -e "Column1\tColumn2\tColumn3"
echo -e "Data1\tData2\tData3"This will output:
Column1 Column2 Column3
Data1 Data2 Data3
In shell scripts, echo is often used to display messages or debug information:
#!/bin/bash
echo "Script starting..."
echo "Current directory is $(pwd)"
echo "Script completed."The output of echo can be redirected to files or used as input for other commands:
-
To a File: Redirect the output to a file using
>or>>:echo "This is a test." > output.txt # Overwrites the file echo "Another line." >> output.txt # Appends to the file
-
As Input: Use the output as input for another command using
|(pipe):echo "This is a test." | tr 'a-z' 'A-Z'
This will convert the text to uppercase:
THIS IS A TEST.
The echo command is a fundamental and versatile tool in Unix and Linux environments, useful for displaying messages, debugging scripts, and redirecting output. Its simplicity and ease of use make it an essential part of shell scripting and command-line operations. Understanding the various options and use cases for echo can greatly enhance your productivity and effectiveness when working with the command line.
NAME
echo - display a line of text
SYNOPSIS
echo [SHORT-OPTION]... [STRING]...
echo LONG-OPTION
DESCRIPTION
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
--version
output version information and exit
If -e is in effect, the following sequences are recognized:
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\e escape
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\0NNN byte with octal value NNN (1 to 3 digits)
\xHH byte with hexadecimal value HH (1 to 2 digits)
NOTE: your shell may have its own version of echo, which usually supersedes the version described here. Please refer to your
shell's documentation for details about the options it supports.