fbpx

How To Use The Echo Command In Linux

A computer system juggles multiple tasks simultaneously. Have you ever pondered over how a computer handles these myriad responsibilities? An operating system like Linux efficiently manages all these tasks for the computer.

Linux heavily relies on commands for user interaction. One such ubiquitous command is the “echo command.” Put simply, the echo command in Linux is utilized to showcase messages on the terminal screen. This piece aims to provide a comprehensive understanding of the echo command, covering its syntax and offering practical examples. Additionally, it delves into displaying variable values and command outputs. Dive into this illuminating article for more insights. 

Syntax of Echo Command

Similar to receiving instructions on the cover page of an exam paper, syntax consists of a set of rules for utilizing commands. In this section, we will explore how to utilize the echo command in Linux. Initially, let’s examine the syntax for the echo command and then delve into each term comprehensively.

Syntax: echo [option] [string]

The term “option” is specifically employed to modify the behaviour of the echo command in Linux. Following a brief example, we will provide an in-depth analysis of Linux echo command options.

Now, our objective is to have the computer showcase the message “hello” on the screen. We will implement the above syntax in the subsequent manner.

Command: echo Hello, World!

Result: Hello, World!

[root@india src]# echo Hello, World! Hello, World!
[root@india src]#

Echo Command Options

As pledged, we shall explore echo command output choices to scrutinize the diverse outcomes. It is crucial to mention that the “-” precedes each option. Presently, we possess the subsequent alternatives that could influence the outcomes.

“-n”: This choice eradicates a trailing newline. In this context, the phrase “newline” denotes a line break or the termination of a line. Thus, upon utilizing this choice, the message will be showcased without transitioning to a fresh line. : This option eliminates a trailing newline. Here, the term “newline” is a line break or the end of a line. So, when you use this option, it will display the message without moving to a new line.

Command: echo -n “Good morning, folks!”

Result: Good morning, folks!

[root@india src]# echo -n "Good morning, folks!"
Good morning, folks! [root@india src]#

“-E”: This option is used to remove escape sequences that impact the final result’s formatting. This option can be considered inversely proportional to “-e.”

Command: echo -E “Good morning,\\n folks!”

Result: Good morning,\n folks!

In the above result, it is evident that the line break after the word “morning” is removed due to the use of “-E”.

“-e”: This option affects how text is displayed on the screen. To apply certain formatting, users must use “\” followed by characters like “t” and “n”.

Command: echo -e “Good \nmorning,\tfolks!”

Result: Good morning, folks!

[root@india src]# echo -e "Good \nmorning, \tfolks!" Good
morning,
folks!
[root@india src]#

In the above result, “\n” instructs the computer to display the text with a line break. It is important to distinguish between “\n” and “-n” as they serve different purposes in creating a new line. The final expression in the string is “\t”, which functions as a tab in the output, creating a space between the words “morning” and “folks.”

Refer to the list of escape sequences used with the Linux echo command option “-e.”

  • \\: Outputs a backslash (\) character in the string.
  • \a: Triggers a sound alert, such as a beep, when the output is displayed.

Command: echo -e “Good morning, folks!\aBEEP\a sound!”

Result: Good morning, folks! Sound!

[root@india src]# echo -e "Good morning, folks! \a BEEP \a sound!" Good morning, folks! BEEP sound!
[root@india src]#
  • \b: Produces a character similar to pressing the “backspace” key.

Command: echo -e “Good\b \bmorning”

Result: Good morning

[root@india src]# echo -e "Good\b \bmorning" Goomorning
[root@india src]#
  • \c: Skips any output entailing this escape character. In the below example, you will notice that the word “folks!” is omitted due to it being mentioned after the escape sequence “\c.”
  • Command: echo -e “Good morning.\c folks!.”

Result: Good morning

Good morning. [root@india src]# echo -e "Good morning.\c folks.\n"
Good morning. [root@india src]#
  • \v: Used to display vertical tab spaces with the string.
    Command: echo -e ‘Good \vmorning, \vfolks!’
Result: Good 
Morning,
 Folks!
[root@india src]# echo -e 'Good \vmorning, \vfolks!'
Good
morning, folks! [root@india src]#

Displaying Variable Values

In programming, variables can be categorized into different types based on their characteristics and the type of data they can hold. There are various types of variables, including:

Numeric Variables:

These variables store numbers. Integer variables hold whole numbers, both positive and negative, without decimal points. Float or Double variables hold numbers with decimal points, with doubles being more precise than floats.

Character Variables:

These variables store individual characters, such as letters or symbols. They can also store small integer values in some programming languages.

String Variables:

These variables store sequences of characters, which can be words, sentences, or any text. They are commonly used for text manipulation.

Boolean Variables:

These variables can only have two values: true or false. They are often used for decision-making and conditional statements.

Array Variables:

These variables store collections of data elements of the same data type. Arrays can be one-dimensional (lists), two-dimensional (matrices), or multi-dimensional.

Pointer Variables:

These variables store memory addresses of other variables. They are commonly used for dynamic memory allocation and working with data structures.

Struct or Object Variables:

These variables can store multiple values, each with its own data type, grouped as a single unit. They are used to represent more complex data structures.

Enumeration Variables:

These variables define a set of named integer constants, allowing you to create symbolic names for integer values.

Global Variables:

These variables are declared outside of any function or scope and can be accessed from anywhere in the program. They have a global scope.

Local Variables:

These variables are declared within a specific function or scope. They have a local scope and are only accessible within that scope.

Constant Variables:

These variables have values that cannot be changed once assigned. They are often used to define values that should remain constant throughout the program.

Environment Variables:

These variables exist at the operating system level and are used to configure the environment for various programs and services. Examples include PATH and HOME in Unix-like systems.

Displaying Command Outputs

The echo command in Linux and Unix-like systems features “command substitution.” This allows you to execute a command within the echo statement and display its output as part of the text that the echo command outputs. By doing this, you can include the results of other commands in the output of the echo command.

Command: echo “This is the list of all the guests in the folder wedding: $(ls)”

[root@india src]# echo "This is the list of all the guests in the folder wedding: $(ls)" This is the list of all the guests in the folder wedding: abc.txt.gz archive.tar [root@india src]#

Through the above command, the computer will display the list of guests mentioned in the “wedding” folder. To list the contents of a directory, you can use the command substitution “$(ls).” By enclosing it within $(…), you direct the shell to execute the ‘ls’ command on a particular directory like “wedding” and incorporate the resulting output in your message. The output will include a list of files and directories present in the specified folder.

Conclusion

The Linux echo command is a vital tool that enables users to showcase messages and information on the terminal screen. Understanding its syntax and options like -n, -E, and -e is crucial for effective use. These options assist in managing the formatting and content of your output. Command substitution can be utilized to include the outcomes of other commands in your echo statements, allowing for the creation of dynamic and informative messages.

We also discussed variables and various types encountered in computer programming. By incorporating command outputs in echo statements, you can exhibit lists, directories, and more. The echo command serves as a versatile tool for efficient communication in the Linux command line, facilitating better interaction with your system and users.