echo Command In Linux Tutorial

The echo command is used to display lines of text in Linux and Unix operating systems. It is generally used to print some text to the standard output which the current terminal. The echo command is very simple but provides different formatting options inside text. The echo command can be also used to print bash variables etc.

echo Command Syntax

The echo command has the following syntax.

echo OPTION VALUE
  • OPTION is used to change echo command options.
  • VALUE can be a string literal or bash variable to print its value.

Print Bash Variable

One of the most popular use cases for the echo command is printing a bash variable. In the following, we print variables named $name , $age .

$ name="İsmail Baydan"

$ age=38

$ echo $name

$ echo $age

Print String Literal

String literals can be printed to the terminal like below.

$ echo "İsmail Baydan"

Interpret Backslash

The backslash is used to remove a previous character from a string. The echo command can use the backslash with the -e option.

$ echo -e "I \blove \bLinux \bTect"
IloveLinuxTect

Suppress Trailing New Line

Trailing newlines can be suppressed with the \c like below by using the echo command.

$ echo -e "I love \cLinux Tect"
I love

New Line

New lines inside the output can be expressed with the \n like below.

$ echo -e "I\nlove\nLinux\nTect"
I
love
Linux
Tect

Horizontal Tab

Tabs or more technically the horizontal tabs can be used like below with the echo command.

$ echo -e "I\tlove\tLinux\tTect"
I	love	Linux	Tect

Carriage Return

Carriage returns can be used like below.

$ echo -e "I\rlove\rLinux\rTect"

Vertical Tab

Vertical tabs can be used like below.

$ echo -e "I\vlove\vLinux\vTect"
I
 love
     Linux
          Tect

Alert

The echo command can create a very basic bip sound as alert like below.

$ echo -e "\aI love Linux Tect"

List Files and Directories

An interesting feature of the echo command is the ability to list the current working directory files and directories.

$ echo *

List Files and Directories with Name Pattern

The echo command can also list files or directories with name patterns. In the following example, we list the *.txt files with the echo command.

$ echo *.txt

Prevent New Line

After the echo command is executed automatically a new line is added by default. This new line can be prevented with the -n option.

$ echo -n "LinuxTect"
See also  How To Generate Random Number In Bash Linux?

Leave a Comment