15 Practical Examples of ‘echo’ command in Linux

echo is one of the most commonly and widely used built-in command for Linux bash and C shells, that typically used in scripting language and batch files to display a line of text/string on standard output or a file.

The syntax for echo is:

1. Input a line of text and display on standard output

Outputs the following text:

2. Declare a variable and echo its value. For example, Declare a variable of x and assign its value=10.

echo its value:

Note: The ‘-e‘ option in Linux acts as interpretation of escaped characters that are backslashed.

3. Using option ‘\b‘ – backspace with backslash interpretor ‘-e‘ which removes all the spaces in between.

4. Using option ‘\n‘ – New line with backspace interpretor ‘-e‘ treats new line from where it is used.

5. Using option ‘\t‘ – horizontal tab with backspace interpretor ‘-e‘ to have horizontal tab spaces.

6. How about using option new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously.

7. Using option ‘\v‘ – vertical tab with backspace interpretor ‘-e‘ to have vertical tab spaces.

8. How about using option new Line ‘\n‘ and vertical tab ‘\v‘ simultaneously.

Note: We can double the vertical tab, horizontal tab and new line spacing using the option two times or as many times as required.

9. Using option ‘\r‘ – carriage return with backspace interpretor ‘-e‘ to have specified carriage return in output.

10. Using option ‘\c‘ – suppress trailing new line with backspace interpretor ‘-e‘ to continue without emitting new line.

11. Omit echoing trailing new line using option ‘-n‘.

12. Using option ‘\a‘ – alert return with backspace interpretor ‘-e‘ to have sound alert.

Note: Make sure to check Volume key, before firing.

13. Print all the files/folder using echo command (ls command alternative).

14. Print files of a specific kind. For example, let’s assume you want to print all ‘.jpeg‘ files, use the following command.

15. The echo can be used with redirect operator to output to a file and not standard output.

echo Options

echo command

That’s all for now and don’t forget to provide us with your valuable feedback in the comments below.

Leave a Reply