Echo Command in Linux is one of the most used commands in Bash scripting. It is used to display the text that has been passed as argument for the command. Mostly it is used as a input for the next command or to send output to a file.
Syntax for using the Echo command in Linux is,
# echo option string
A simple example would be,
# echo “this is a test command”
Now let’s discuss some the most commonly used & important option of Echo command in Linux with the help of some examples.
Echo Command in Linux
1- Simple text display to screen or to a file
To simply send text as output to screen, run
# echo HELLO WORLD
Same output can also be sent to a file by using the redirection symbol,
# echo HELLO WORLD > echo_output.txt
2- Print files and folders
You might have never used echo command to display all the files or folders in the current directory, but it can do so by using the following command,
# echo *
We can only display files with an extension type,
# echo *.html
3- Remove trailing newline
To remove the trailing newline from the output of echo command, we can use ‘n’ option,
# echo -n HELLO WORLD
4- Extra options with use of ‘e’ command
Echo command when used with option ‘e’ allows for the interpretation backslash escapes & one of those can be used to sound an alert when executed.
# echo -e “Hello \aWorld”
All the backslash escapes that can be used apart from ‘\a’ are,
\\ display a backslash when using option ‘e’
\b display backspace character
\c produce no further output
\e display escape character
\f display form feed character
\n create a new line
\r displays a carriage return
\t create a horizontal tab
\v creates a vertical tab
That’s it guys, this completes our tutorial on how to use ECHO command in Linux. Please feel free to send in any questions or queries using the comment box below.