Linux echo Command: Beyond the Basics
The echo
command in Linux allows you to display text or output data to the terminal.
It can be used for various purposes such as printing the value of a variable, generating formatted output, or even debugging a script.
Try this command in your terminal:
echo "Hello, world!"
Here, “Hello, world!” is printed to the terminal. The echo
command takes the string as its argument and prints it to the standard output, which is your terminal by default.
- 1 What is standard output?
- 2 Syntax and options
- 3 Echoing variables
- 4 Print special characters
- 5 Suppress trailing newline
- 6 Disable interpretation of backslash escapes
- 7 Interpretation of backslash escapes
- 8 Printing colored output with ANSI escape codes
- 9 Redirection of echo output to a file
- 10 Appending to a file
- 11 Pipe echo output to another command
- 12 Quotation marks with echo
What is standard output?
Standard output, often abbreviated as stdout, is the default data stream where a program writes its output data.
In Linux and Unix-like systems, the standard output is the screen or the terminal by default.
Let’s take a look at a simple command:
echo "This is standard output"
This command will print “This is standard output” to your terminal. By default, the echo
command sends its output to stdout.
Syntax and options
The general syntax for the echo
command is:
echo [option]... [string]...
The echo
command supports the following options:
-n
: This option prevents the trailing newline. By default,echo
appends a newline character at the end of the output. Using-n
suppresses this behavior.-e
: This option enables the interpretation of backslash escapes. It allows you to use special characters such as\n
for a newline,\t
for a tab, and so on.-E
: This option disables the interpretation of backslash escapes. It is the default behavior ofecho
, but can be used for clarity or to override any default changes in different environments.
Echoing variables
In bash scripting, you can create variables to store information. With the echo
command, you can display the contents of these variables.
First, let’s create a variable:
name="John Doe"
Now, let’s print the value of this variable:
echo $name
Here, “John Doe” will be printed to your terminal.
By using the $
sign before the variable name ($name
), you’re telling Bash that you want to display the contents of the name
variable.
You can also display the value of environment variables using echo:
echo $USER
This will print the currently logged user.
Print special characters
You can display these special characters with the echo
command by using the backslash (\
) which is called an escape character.
It tells Bash to treat the next character as a literal character, not as a special character.
In Bash, special characters are characters that have a specific function. For example, the $
sign is used for variable substitution.
Let’s try outputting a dollar sign:
echo "\$100"
This command will print “$100” to your terminal.
Suppress trailing newline
By default, the echo
command adds a newline character at the end of its output. If you want to suppress this newline, you can use the -n
option.
echo -n "No newline here"
After executing this command, “No newline here” is printed, but the cursor stays on the same line.
Disable interpretation of backslash escapes
The -E
option disables the interpretation of backslash escapes, which is the default behavior of echo
. This means that special characters will be treated as plain text.
echo -E "Hello\nWorld"
This command will print “Hello\nWorld” because -E
prevented the echo
command from interpreting \n
as a newline character.
Interpretation of backslash escapes
The -e
option enables the interpretation of backslash escapes. This allows you to use special characters like \n
for a new line, \t
for a tab, and so on.
echo -e "Hello\nWorld"
This command will print:
Hello World
By using -e
, the echo
command interpreted n
as a newline character, which is why “Hello” and “World” are printed on separate lines.
Alert (bell)
The \a
character triggers a beep sound, or an alert in your terminal. It’s also called a bell character.
echo -e "\a"
When you run this command, you’ll hear a beep sound.
Backspace
The \b
character is interpreted as a backspace. It removes the previous character.
echo -e "Helloo\b World"
This command will print “Hello World”. The extra ‘o’ was removed by the backspace character b
.
Suppress further output
The \c
character suppresses any further output on that line.
echo -e "Hello\c World"
This command will print “Hello” and then suppresses the rest of the line, so “World” doesn’t appear.
Escape
The \e
character is the escape character. It can be used to start escape sequences for other characters.
echo -e "\e"
This command just prints an empty line because \e
itself doesn’t produce any visible output.
It’s generally used in combination with other characters in escape sequences such as printing colored output as we’ll see later in this tutorial.
Form feed
The \f
character is the form feed character. It’s used to move the cursor to the beginning of the next page.
However, since the terminal doesn’t have pages, \f
just moves the cursor to a new line.
echo -e "Hello\fWorld"
This command will print:
Hello World
New line
The \n
character is the newline character. It’s used to start a new line.
echo -e "Hello\nWorld"
This command will print:
Hello World
The n
character started a new line after “Hello”.
Carriage return
The \r
character is the carriage return. It moves the cursor back to the beginning of the line.
echo -e "Hello\rWorld"
This command will print “World”. The carriage return \r
moved the cursor back to the beginning of the line after printing “Hello”, and “World” was printed at the start of the line, overwriting “Hello”.
Horizontal tab
The \t
character is the horizontal tab. It inserts a tab space.
echo -e "Hello\tWorld"
This command will print “Hello World”. The \t
character inserted a tab space between “Hello” and “World”.
Vertical tab
The \v
character is the vertical tab. It starts a new line and adds some spaces before the next character.
echo -e "Hello\vWorld"
This command will print:
Hello World
Backslash
The \\
sequence represents a literal backslash.
echo -e "Hello\\World"
This command will print “Hello\World”.
The character whose ASCII code is nnn (octal)
You can use \0nnn
to represent a character by its ASCII code in octal format. Make sure to replace nnn
with the octal ASCII code of the character you want to print.
echo -e "\0061"
This command will print “a”. The ASCII code for “a” is 61 in octal format, so \0061
represents “a”.
Printing colored output with ANSI escape codes
You can use ANSI escape codes to print colored output in the terminal. ANSI escape codes start with \e[
followed by some parameters, and end with m
.
Let’s print “Hello, world!” in red:
echo -e "\e[31mHello, world!\e[0m"
This command prints “Hello, world!” in red. The escape code \e[31m
changes the text color to red, and \e[0m
resets it back to the default color.
Redirection of echo output to a file
You can use the >
operator to redirect the output of echo
to a file.
echo "Hello, world!" > hello.txt
After running this command, a file named “hello.txt” is created, and “Hello, world!” is written to this file. If the file already existed, its contents are replaced with “Hello, world!”.
Appending to a file
The >>
operator can be used to append the output of echo
to a file.
echo "Hello again!" >> hello.txt
This command adds “Hello again!” to the end of “hello.txt”. It doesn’t overwrite the existing content, it just appends to it.
Pipe echo output to another command
The |
(pipe) operator is used to pass the output of one command as the input to another command.
echo "Hello, world!" | wc -w
This command prints “2”. The echo
command produces the string “Hello, world!”, which is then piped into the wc -w
command.
The wc -w
command counts the number of words in the input, so it prints “2”.
Quotation marks with echo
When you use variables or escape sequences with echo
, remember to use quotation marks.
name="John Doe" echo "$name" echo -e "Hello\nWorld"
The first command prints “John Doe”, and the second command prints:
Hello World
Without quotation marks, variables wouldn’t be expanded, and escape sequences wouldn’t be interpreted.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.