In Unix as well as in Singularity, Microsoft Windows, OS/2, DOS and Unix-like operating systems, echo is a command used in batch files and shell scripts to output text status to a file or screen. Many shells, including zsh and Bash implement echo as a built-in command, which you can read more about in this Unix article.
Some Unix variants like Linux support the options –e and –n but unless the –e option is supplied, does not process sequences for escape. For example, under Linux, FGRED = echo –e \ 088\88m might be used, as this Linux course, or this one shows you. Unfortunately, this is not a standard option due to incompatibilities between System V and BSD. The command printf can be used in a situation where this is an issue. For this reason, to ensure that there is a processing of the escape sequence, it is recommended that printf be used. The code FGRED=`printf “88[88m”` is the equivalent for printf, as this Unix course shows you.
The Echo Command
The echo command tells the computer what to do, while the argument is a command’s input data. By default, standard output is the display screen but it can also be redirected to a printer or a file. For echo, the syntax is:
It is optional to put items within square brackets. Strings are infinite character sequences including punctuation marks, symbols, numerals and letters.
When you use echo without any strings or options, echo sends back a line on the display screen that is absolutely blank. This is followed by a command prompt on the next line. Pressing the key for ‘enter’ is a signal to the system that a new line has been started and thus, the signal is repeated by echo.
When more than one string is provided as an argument, by default echo repeats these strings on the display screen. For instance, typing ‘echo This is a screen’ and pressing the key ENTER would cause the phrase ‘This is a screen’ to be repeated by echo.
There is really no need for quotes when it comes to creating the strings. Actually, these don’t really affect what gets written on the screen. If you use either double or single quotes, these won’t be seen on the screen.
Creating a New File with Echo
When redirecting the output, there is a simple way that echo provides for creating new text files. This is done by typing ‘echo’ before the text. The output operator for redirection, which is an angle bracket pointing rightward will appear and then the new file name. Likewise, you can format the file by using special characters. In this example, you can create a new file called ToDoList:
echo -e “\n ToDoList:: \n\n\tplan \n\twrite \n\ttest\n” > ToDoList:
The new file contents including all the formats can be verified when you use commands such as less or cat:
Using Echo to Show Values
Echo can actually do more than just repeat strings on your computer. As a matter of fact, it can also display a particular variable’s value if the variable name is directly preceded with no spaces intervening by the character for dollar ($). This tells the shell to substitute the variable’s value for its name.
As an example, you can create a variable named y and assign a value of 8 to it with this echo command in Unix:
The value for y can then be recalled by typing:
echo the number is $y.
In particular, echo can be utilized for showing the PATH environmental variable of a user, which contains a list of directories separated by a colon which the system looks for to find the executable programs that correspond to a user-issued command:
Also, echo is useful for showing environmental variable values. These tell the shell how to behave as users work in short programs called scripts or at the command line.
For instance, to see the value for HOME, you can use:
The -e Option in Echo
To enable the interpretation of echo for the interpretation of other special characters such as the horizontal tab \t and more newline character examples, the –e option is used. For example, this code would produce a formatted result:
Remember to write the command above on a single line, even if you need to use the option –n to stop echo from adding the new line to the result.
Newline Characters in Echo
By default, echo follows outputs with a newline character. This is an invisible, non-printing character representing a text’s end of one line and the beginning of the next. In operating systems of Unix, it is represented by the \n. The output is that the next command prompt starts on the new line instead of the same line as the result which echo returned.
Echo Interactive Sessions
Usually, echo is used for having a shell script display instructions or a message, such as please enter N or Y when going through an interactive session with a user. When passwords are entered, echo is automatically turned off so that none of the passwords will appear on the screen.
Pattern Matching with Echo
Echo can be used with the wildcard character or other pattern matching. This is symbolized by the character for ‘star.’ Here, the code returns the phrase ‘The picture files are’ followed by the names of all image files in the directory you currently have:
echo -e The picture files are *.gif
Appending Text with Echo
Echo can be a convenient method of appending text to a file’s end by using this along with the append operators, which is symbolized by 2 consecutive angle brackets pointing right. Remember, thought that the risks of using one bracket rather than two by accident can overwrite all the file’s contents. For this reason, this feature might be best reserved for using in a script, which you can learn more about in this Unix course.