Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
Linux

How To Use The Echo Command In Linux?

Introduction

A computer system manages many tasks in a single time frame. Have you ever thought about how a computer manages all these duties? An operating system such as Linux handles all such activities for the computer. 

The Linux system is majorly dependent on commands for interaction. One such commonly used command is the “echo command.” In much simpler terms, the echo command in Linux is used to make the computer display messages on the terminal screen. This article will help you in getting a broader perspective of echo command along with its syntax and examples. Also, you will get to know about display variable values and command outputs. So, carry on reading this enlightening piece of article. 

Syntax of Echo Command

Just like you would get instructions on the front page of an exam paper, syntax is a set of rules to use commands. Under this headline, we will learn to use the echo command in Linux. Let’s first look at the syntax for the echo command and then understand each term in detail.

Syntax: echo [option] [string]

The term “option” is specifically used to alter the behavior of the command echo in Linux. After a short example, we will have a detailed view of Linux echo command options.

Now, here, we want the computer to display the message “hello” on the screen. We will use the above syntax in the following way.

Command: echo Hello, World!

Result: Hello, World!

Echo Command Options

Echo Command Options

As promised, we will discuss echo command output options to analyze the various results. An important thing to note here is that the “-” is used before each option. Now, we have the following alternatives that can impact the results.

“-n” : 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!

Echo Command Options

“-E” : This option is used to eliminate the escape sequences that affect the formatting of the final result. You can say that this option is inversely proportionate to “-e.”

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

Result: Good morning,\n folks!

Echo Command Options

In the above result, it is clear that due to the usage of “-E,” the line break after the word “morning” is eliminated.

“-e” : This option works on the display of the text on the screen. To achieve specific formatting, the user needs to use a “\” followed by characters such as “t” and “n”.

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

Result: Good morning, folks!

Echo Command Options

In the above result, “\n” instructs the computer to show the text with a line break. One must keep in mind that “\n” and “-n” perform different functions in regard to a new line. The last expression in the string is “\t”. This expression is used as a tab in the result, as you can see that there is a gap between the word “morning” and “folks.”

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

  • \\: Produces a backslash (\) character in the string.
  • \a: Makes a sound alert like a beep when displaying the output. 

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

Result: Good morning, folks! Sound!

Echo Command Options
  • \b: Produces a character similar to pressing the “backspace” key.

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

Result: Goodmorning

Echo Command Options
  • \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

Echo Command Options
  • \v: Used to display vertical tab spaces with the string. 

Command: echo -e ‘Good \vmorning, \vfolks!’

Result:       Good 

   morning, 

     folks!

Echo Command Options

Also Read: 14 Tar Command in Linux

Display Variable Values

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

1. Numeric Variables:

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

2. Character Variables:

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

3. String Variables:

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

4. Boolean Variables:

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

5. 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.

6. Pointer Variables:

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

7. 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.

8. Enumeration Variables:

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

9. 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.

10. Local Variables:

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

11. Constant Variables:

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

12. Environment Variables:

These are variables that 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.

Display Command Outputs

The echo command in Linux and Unix-like systems has a feature called “command substitution.” This allows you to run a command within the “echo” statement and show its output as part of the text that the “echo” command prints. By doing this, you can include the result 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)”

Display Command Outputs

Through the above command, the computer will show the list of all the guests mentioned inside the folder “wedding.” To list the contents of a directory, you can use the command substitution “$(ls).” By enclosing it within $(…), you instruct the shell to run the ‘ls’ command on a specific directory such as “wedding” and include the resulting output in your message. The output will consist of a list of files and directories that are present inside the specified folder.

Also Read: How to Master the Linux Curl Command

Conclusion

The Linux echo command is a crucial tool that allows users to display messages and information on the terminal screen. To use it effectively, it’s important to understand its syntax and options like -n, -E, and -e. These options help you control the formatting and content of your output. You can use command substitution to include the results of other commands within your echo statements. This lets you create dynamic and informative messages. 

We also looked at variables and different types you might encounter in computer programming. You can incorporate command outputs within echo statements to display lists, directories, and more. The echo command is a versatile tool that can help you communicate effectively in the Linux command line. It enables you to interact with your system and users more efficiently.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles