5 most used echo commands in linux with examples

5 Most Used Echo Commands in Linux With Examples

Spread the love

This blog post will show you the five most used echo commands in Linux with examples.

The echo command in Linux is a command that outputs the strings passed to its arguments. The echo command is supported on various Linux distributions such as Ubuntu, CentOS, Debian, and other OS like Windows. This command is most used in shell scripts and batch files to output status text to the screen.

In this tutorial, we will use the latest Ubuntu 22.04 OS, but you can choose any Linux distro per your choice. Let’s get started!

Prerequisites

  • A server with Ubuntu 22.04 OS
  • User privileges: root or non-root user with sudo privileges

Update the System

It is recommended to update the system packages to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Once the system is updated, we are ready to show you the five most used echo commands in Linux.

Syntax of the echo command

The syntax of the echo command is the following:

echo [option(s)] [string(s)]

The echo command is simple and is being executed with the main word echo using some options and strings next to it.

1. Display simple line to standard output

To display a single line as text on standard output, execute the following echo command with the string:

echo This blog post will teach you about the echo command.

This is how looks the output of this command:

root@host:~# echo This blog post will teach you about the echo command.
This blog post will teach you about the echo command.

2. Print Files and Folders

The echo command can be used to print all files and folders in some directory as an alternative to the ls -al command for listing.

echo *
This is the output of the command above:
root@host:/# echo *
bin boot core dev etc home lib lib32 lib64 libx32 lost+found media mnt opt proc root run sbin snap srv sys tmp usr var

3. Redirect operator to output text in a file

The following command will redirect the output in the file instead of the standard output on the command line.

echo * > outputfile.txt

To check the content of the file, execute the cat outputfile.txt command, and you should receive the same as in the previous paragraph:

root@host:/# cat outputfile.txt
bin boot core dev etc home lib lib32 lib64 libx32 lost+found media mnt opt proc root run sbin snap srv sys tmp usr var

4. Removing Text Spacing

To remove the text spacing in some lines, execute the following command with option -b:

echo -e "This \bblog \bpost \bwill \bhelp \byou \bwith \understanding \bthe \becho \bcommand"

You should get the following output:

root@host:/# echo -e "This \bblog \bpost \bwill \bhelp \byou \bwith \bunderstanding \bthe \becho \bcommand"
Thisblogpostwillhelpyouwithunderstaingtheechocommand

5. Displaying the Text in Multiple Lines

To display the text in multiple lines, execute the following command with option \n.

echo -e "This \nblog \npost \nwill \nhelp \nyou \nwith \nunderstanding \nthe \necho \ncommand"

You should get the following output:

root@host:/# echo -e "This \nblog \npost \nwill \nhelp \nyou \nwith \nunderstaing \nthe \necho \ncommand"
This
blog
post
will
help
you
with
understanding
the
echo
command

These were the most used echo commands on a daily basis by system administrators and other Linux users. If you want to know more about the echo command, just execute the command man echo, and you will get a complete definition of echo and its usage:

root@host:/# man echo
ECHO(1)                                                                    User Commands                                                                    ECHO(1)

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:

       .
       .
       .
       .
       .

That’s it. You just learned to use the echo command on Linux with examples. Of course, if you need some help with this, you just need to sign up for one of our NVMe plans and submit a support ticket. Our admins will start work on your request immediately. We are available 24/7.

If you liked this post about the five most used echo commands in Linux with examples, please share it with your friends on social networks or simply leave a reply below.

One thought on “5 Most Used Echo Commands in Linux With Examples

  1. The echo command is one of the most fundamental and versatile tools in the Linux command-line arsenal. It’s primarily used for displaying text messages to the terminal, but it also offers various options for manipulating and formatting output.

Leave a Reply

Your email address will not be published. Required fields are marked *