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 in Linux 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 the 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 Linux
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 the 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 them and fix them 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:
- Input a string of text and get a display o (of) standard output through the $ button.
- Make a variable and echo its value, such as choosing a variable like x and giving it the value=20.
- Use the \b option with the backslash interpreter -e, which will take away the spaces between them.
- Use the \n option, which creates a new line, with the backspace interpreter - to make a new line.
- Take \t to create a horizontal tab and then use it with the -e backspace interpreter to create spaces in horizontal tabs.
- Use the horizontal tab \t with the new line \n command.
- Get vertical tab spaces using backspace interpreter -e and vertical tab option with \v.
- Use \v and the new line \n at the same time.
- Try the backspace interpreter -e with the \r carriage return option to get a specific carriage returned in the generated output.
- Suppress trailing new lines with \c and the backspace interpreter -e to move on without giving a new line.
- Take away the trailing of new lines using the -n option.
- Use the alert return \a option with -e in order to get a sound alert.
- Print any folder or file using the $ echo command.
There are a seemingly endless number of things you can do with the echo command in Linux, 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.