en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. How to Use the echo Command in Linux + Examples

How to Use the echo Command in Linux + Examples

In this article, we will explain how to use the echo command in Linux, and we will provide some examples that will help you understand it better and how to use it.

Linux Tutorial Apr 06, 21 by Nisal N 6 min Read
How to Use the echo Command in Linux + Examples

Ideal for writing formatted text within the terminal window, echo commands are pretty helpful. It doesn't even have to be restricted to static text. Instead, it can also include shell variables, directories, and filenames at the same time. You could also choose redirect echo to help create and then log text files.

As the name suggests, echo repeats whatever you tell it to do, existing solely to repeat whatever it is said to repeat. While this seems like quite a simple function, it is, in fact, a vital one. 

Without using echo, as an example, it would be difficult to do things like obtaining visible output from any shell script. Though it isn't exactly fancy, there's a high likelihood that echo has some good capabilities that you can get out of it for your personal use.

Most Linux systems come with two different echo versions, which are accessed using the two commands: 

  • type echo 
  • whereis echo 

The type command will show you if the command given to it is a binary executable, a shell builtin, a function, or an alias. 

Once it has found an answer, the type function will stop looking for any more matches, so it won't tell you if there are other commands with a similar name.

When you use the whereis command, it will look for the source code and binary executable for the command that it is given, using it as its command-line parameter. It won't look for shell builtins since these don't have their separate binary executable; instead, they're a part of the Bash executable.

Writing Text to the Terminal

To write a simple line of text to the terminal, type 'echo' and then the words you want it to show. The text will then be repeated for you. 

Sometimes, if you input an apostrophe, it will interpret that as a sign that there are more things you're waiting to type and is expecting further input. 

In order to make sure that doesn't happen if you're using an apostrophe, enclose the line of text in a set of speech quotes ("like this").

Doing this in any instance of using echo helps outline the parameters you are using, so that echo can help you read and debug the scripts more easily. 

Furthermore, if you want to put more than one quoted line in your texts, place a backslash \ in front of the double quotation mark. Make sure you have no space in between then.

Using Variables With echo

You can use variables through echo in order to produce something that has values and is more dynamic. For instance, if you type my_name="John" —then the variable my_name has been created, and the text "John" is its value. The variable name can then be used in the strings of text you give to echo. Be sure you put a dollar sign $ in front of the variable to inform echo that it is indeed a variable.

Using Commands With echo

Using commands with echo will help incorporate the output into the text line that you wrote to the terminal window. Again, the dollar signs $ must be used as if the command was a variable, and then you can enclose the entire command in parentheses. 

Something to consider is the fact that you should use the commands on their own before you begin to use them with echo. By doing so, if there are any issues with your syntax, you can find it and fix it before you try to put it through echo. 

In this way, if echo doesn't do what you think it should, you'll know that the problem is with the syntax since you've already proven that.

Formatting Text With echo

There are a number of text commands you can use to format the information using some backslash-escaped elements to configure how the text is laid out. Some of the most commonly used ones include:

  • \a: Alert. This will create an alert sound.
  • \b: This will generate a backspace character.

  • \c: This will get rid of any additional output.

  • \e: This will create a new escape character.

  • \f: This will make and write a feed character.

  • \n: This will create a new line.

  • \r: This will make a carriage return.

  • \t: This will create a horizontal tab.

  • \v: This will create a vertical tab.

  • \\: This will generate a backslash character.

Note that before using the commands above, be sure to have the syntax as follows:

echo -e "your text goes here."

Examples of echo in Use:

  1. Input a string of text and get a display o (of) standard output through the $ button.
  2. Make a variable and echo its value, such as choosing a variable like x and giving it the value=20.
  3. Use the \b option with the backslash interpreter -e, which will take away the spaces between them.
  4. Use the \n option, which creates a new line, with the backspace interpreter - to make a new line.
  5. Take \t to create a horizontal tab and then use it with the -e backspace interpreter to create spaces in horizontal tabs.
  6. Use the horizontal tab \t with the new line \n command.
  7. Get vertical tab spaces using backspace interpreter -e and vertical tab option with \v.
  8. Use \v and the new line \n at the same time.
  9. Try the backspace interpreter -e with the \r carriage return option to get a specific carriage returned in the generated output.
  10. Suppress trailing new lines with \c and the backspace interpreter -e to move on without giving a new line.
  11. Take away trailing of new lines using the -n option.
  12. Use the alert return \a option with -e in order to get a sound alert.
  13. Print any folder or file using the $ echo command.

Conclusion

There are a seemingly endless number of things you can do with echo, making it a compact and lightweight tool that packs a deceptively powerful punch for your Linux system.

Now that we got through the basics of the echo command, go and check the following:

Let us know what you think in the comments below.

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment