If you’re working in Linux, Echo is one of the most prevalent, commonly-used built-in commands for both bash and C shells. This is a basic command that’s used in most operating systems where a command line is required.
Echo is commonly used in scripts, batch files or as a component of individual commands – in other words, in any instance where you need to add or insert text. That’s why echo is a built-in command in shells such as bash, ksh and csh.
Terms and Syntax For Echo
First, we should get clear on a couple of terms:
Echo also is used with tags, such as:
Examples of Echo
Now that we’ve gone through a little cheat sheet for echo, here are a few examples of how it’s used.
To print “I need a sandwich” on console:
$ echo “I need a sandwich”
I need a sandwich
This time, we want to print the value of x, where x=10
$ echo $x
This time, we want to remove spaces – this touches on the terms we discussed above, in our cheat sheet:
$ echo -e Here \bthe \bspaces \bare \bbackspaced
Here, we’ll use option \n for newline, with backspace interpreter -e treating each newline from where it is used:
$ echo -e Here \nthe \nspaces \nare \nnewlined
Using option \t – horizontal tab with backspace interpreter -e, to create horizontal tab spaces:
$ echo -3 ‘Here \tthe \tspaces \thave \thorizontal \ttab \tspaces’
Here the spaces have horizontal tab spaces
More On Syntax
We did touch on syntax for Echo briefly above, so let’s expand on that. The syntax for echo is:
Echo [options] [strings]
When no options or strings are specified, echo will display a blank line on the display screen, with a command prompt following on the subsequent line. When you press the ENTER key on your keyboard, this is a signal for the system to initiate a new line, and echo will repeat this signal. When arguments consist of one or more strings, echo will (by default) repeat those strings on the screen. For instance, typing in the following phrase and then pressing the ENTER key would result in echo repeating the phrase I need a sandwich on the screen:
echo I need a sandwich
As you see, it isn’t necessary to use quotation marks to surround this string, as it doesn’t have any effect on what is displayed on the screen. When sing either double or single quotation marks, they will not be repeated on the screen.
Of course, echo can do more than repeat word-for-word whatever follows it. When the dollar character ($) is used and is put before a particular variable with no intervening spaces, it can show the value of that variable by telling the shell to substitute that variable’s value for its name.
Let’s say that you have a variable named x with a value of 5. It can be created and its value set with this command:
Now, we can recall the value of x with echo by the following phrase:
echo The number is $x.
There will be instances where the shell needs to understand how to behave when a user is working at the command line, or in a script (which is actually a short program). These are called environmental variables, and echo is especially useful in these iterations.
Let’s say the current user wants to make reference to the home directory. In that case, the value of HOME is the environmental value:
The user’s PATH can be considered another environmental variable, along with a colon-separated list of the directories that the system accesses to search for an executable program specified by a user’s command:
By default, echo will follow any output with a newline character. This is an invisible, non-printing character, representing the break at the end of one line of text and the start of the next line. In Unix-like systems, it’s represented by |n. As a result, the next command prompt will begin on a new line, rather than remaining on the same line as the output generated by echo.
The -e option is for instances when there are other special characters, such as a horizontal tab (represented by \t) along with additional repeats of the newline character. The -e option comes into play in a formatted output such as this:
echo -e “\n Projects: \n\n\tplan \n\tcode \n\ttest\n”
While a command like the above one might display as two lines on a smaller screen, it should be written as a single line. To prevent echo from adding newline to the output, the -n option should be used.
For redirecting output, echo incorporates a simple way of creating text in a new file. It’s a matter of typing echo and then the desired text, followed by the output redirection operator (a right-pointing angle bracket) and then the name of the new file. In this case, special characters can also be used to format the new file. So, for instance, we can create a new file using the formatted output from the example above:
echo -e “\n Project 1: \n\n\tplan \n\twrite \n\ttest\n” > project1
This new file, including any necessary formatting, can be verified when you use a command such as cat or less, for instance:
Echo is also a convenient way of appending text at the end of a file, when using it in conjunction with the append operator. The append operator is represented by two right-pointing angle brackets. NOTE: this feature should ideally only be used in scripts – if you accidentally use a single angle bracket instead od the double bracket, it can overwrite the entire contents of the file.
Echo can also be used with the wildcard character for pattern matching. We’ll call the asterisk the wildcard character, and this command would return the phrase The gif files are, followed by a list of all the .gif files found in a given directory:
echo -e The gif files are *.gif
Echo is also used for when you need a shell script to display instructions or a message, such as an interactive session with users where a Please enter Y or N option is needed.
NOTE: echo will automatically be turned off whenever a password is entered, so the password won’t be shown on the screen.
Be aware that there are several implementations of echo that exist on different operating systems, some of which may be incompatible. Some will expand escape sequences by default, while others don’t. Some of them also can accept options, varying with implementations, and some of them will not.
With these variations in behavior and performance, echo is regarded as a non-portable command on Unix-type systems. In some cases, the printf command (introduced by Ninth Edition Unix) is the preferable command.