What is the Echo Command?
The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file.
The echo command has various options that can change the format and behavior of the output, such as enabling the interpretation of escape characters, omitting the newline after the output, or changing the color of the text. The echo command can also use variables and commands as arguments, which makes it more versatile and dynamic.
The echo command can also write to files and directories by using redirection operators, such as > or >>. The echo command is one of the most basic and essential commands in Linux, and it can be used for various purposes, such as debugging, testing, or creating text files.
Using Echo and Redirection Operators
Redirection operators are symbols that allow you to change the source or destination of data in Linux commands. There are two types of redirection operators: input and output.
- Input redirection < takes data from a file or another command and feeds it to the standard input of a command.
- Output redirection > takes data from the standard output of a command and writes it to a file or another command.
- There are also variations of output redirection, such as appending >> which adds data to the end of an existing file, and error redirection 2> which redirects the standard error of a command to a file or another command.
Redirection operators are useful for manipulating data and controlling the flow of information in Linux, with examples shown below.
Output Redirection Example 1
In the example below, I’ve used the output redirection while appending the text as I did not wish to overwrite all of the file content.
The text file below had 3 lines and with the command echo “This is the fourth line” >> example.txt I was able to add text to the end of the file.
Output Redirection Example 2
What would happen if I were to use > ?
As shown above, all the file’s content will be removed and overwritten by the single line used with the echo command.