Javatpoint Logo
Javatpoint Logo

Echo Command in Linux/Unix with Examples

In Linux, the echo command can be used for displaying a line of string/text that is passed as the arguments. This command is a built-in that is mostly and widely used in various batch files and shell scripts to outcome status test to a file and screen.

Linux distributions permit us to control and set up the system ourselves. In this context, the echo command is one of the most used commands. It's used in batch files and bash scripts to result in a status text on our screen or in a file. The command repeats what we have been told to repeat. This function is very simple but needs to do only that. For instance, we wouldn't get a visible result from shell scripts without echo. Shell is a user interface in which we can input distinct commands like the cat command, head command, tail command, or else the echo Command.

The common echo operation is equivalent to every programming language. We make the input (which is in the string form), and it is received and results in again unchanged.

Important: The command line interpreter or user interface is also known as the bash shell. It is the standard shell in the distributions of Linux and the text window where we can input our commands. The echo command is an elementary segment of Ubuntu and comes pre-installed on all systems.

Syntax of the echo command


echo command

Implementations of the echo command

The echo command exists in SymbOS, KolibriOS, HP MPE/iX, ReactOS, Microsoft Windows, IBM OS/2, Digital Research FlexOS, Acorn Computers Panos, Microwave OS-9, Zilog Z80-RIO, MetaComCo TRIPOS, TSC FLEX, Multics, Unix-like and Unix operating systems.

Several shells including every Csh-like (like zsh or Bash), Bourne-like shells, and COMMAND.COM as well and cmd.exe execute the echo command as the built-in command.

The command also exists inside the EFI shell.

History of the echo command

The echo command started in Multics. Later it was specified by Doug Mcllroy in C language as the "finger exercise" and verified to be helpful. It became a part of the 2 versions of Unix.

In the 7 versions, the echo -n command replaced prompt (behaved like the echo command, however, without terminating the output along with the line delimiter).

This command began developing C escape sequences like \n along with the difference that the octal escape sequences were represented as the \0ooo rather than \ooo in C on UNIX/PWB and after Unix System 3.The echo command exits in 2 versions on MS-DOS.

Nowadays, various echo incompatible implementations are available on distinct operating systems (various on the similar system). By default, a few of them developing escape sequences, a few of them does not. Similarly, a few of them getting options (list of which changing with implementations), and a few of them does not.

Echo POSIX Specification leaves the nature unspecified when the initial argument is -n or other arguments include backslash characters during the specification of Unix (option XSI in POSIX) mandates the development of the sequences and doesn't permit the option processing.

Several implementations of the echo command are not flexible inside the default platform. Due to these types of changes in nature, the echo command is examined as a non-portable command, and the command, i.e, printf is preferred rather on Unix-like systems.

What are the echo control characters?

Once we have understood the general principle of the echo command, we will learn the control characters that we can use with the command. These control characters are not visible on our screen directly but determine several things, such as the line breaks, the end of the text, and the beginning of the text.

Escape Description
\a Alarm sound
\b A character back
\c Suppresses text wrapping
\f Back
\r Back to the start of the line
\n Line break
\t Tabular (Optional)
\\ Backslash character output
\v Tabular (optional)
\0nnn Octal ASCII characters (only ksk and sh)
\nnn Octal ASCII characters (only bash)

How can colors be determined using echo?

The echo command can be used to define text attributes like colors for the background and font while outputting text. It works by inserting every character in quotes or specifying the colors in several variables to make the string much easier to read.

The following is an overview of distinct escape codes for distinct background and font colors:

Control characters Description
\033[30m Black font color
\033[31m Red font color
\033[32m Green font color
\033[33m Yellow font color
\033[34m Blue font color
\033[35m Magenta font color
\033[36m Turquoise font color
\033[37m White font color
\033[40m Black background
\033[41m Red background
\033[42m Green background
\033[43m Yellow background
\033[44m Blue background
\033[45m Magenta background
\033[46m Turquoise background
\033[47m Grey background

How do we set text properties with echo?

In inclusion to the background and font color, the echo command can be used to define text attributes like underline or boldface. The following are some codes for many text properties:

Control character Description
\033[0m Reset every attribute
\033[1m Font bold
\033[4m Underline
\033[5m Flashing
\033[7m Inverse display

Displaying a string/text:

Syntax:

Example:

Output:

echo command

Echo Command Options

There are various options available in the echo command. These options are listed and explained below:

1. \b: This option is used for removing every space among the text/string.

Example:

Output:

echo command

2. \c: This option is used for suppressing the trailing new line along with the '-e' backspace interpretor to proceed without emitting any new line.

Example:

Output:

echo command

3. \n: This option is used for creating a new line and this new line will be created from where it's used.

Example:

Output:

echo command

4. \t: This option is used for creating the horizontal tab spaces.

Example:

Output:

echo command

5. \r: This option is used for carriage return along with '-e' backspace interpretor to have described carriage return in the result.

Example:

Output:

echo command

6. \v: This option is used for creating the vertical tab spaces.

Example:

Output:

echo command

7. \a: This option is used for alert return along with '-e' backspace interpretor to add sound alert.

Example:

Output:

echo command

This command if run, it will generate a bel or alert sound.

Note: Before firing, ensure to check the volume keys.

8. echo *: This option is used for printing every folder or file. It is the same as the ls command in Linux.

Example:

Output:

echo command

9. -n: This option is used for omitting echoing trailing new lines.

Example:

Output:

echo command

10. Print "Hello All": For printing text "Hello All!" on the terminal, we can use the below command:

Example:

Output:

echo command

11. Print specific types of files: For example, if we wish to print every '.c' file, we can apply the below command:

Example:

Output:

echo command





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA