Chat Zalo Chat Messenger Phone Number Đăng nhập
15 Practical Examples of 'echo' command in Linux - Tecmint

15 Practical Examples of ‘echo’ command in Linux – Tecmint

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

echo commandecho command
examples

The echo command syntax is: echo

[option(s)] [string(s)]

1. Enter a line of text and display it in the standard output

$ echo Tecmint is a Linux Nerds community

It generates the following text:

Tecmint is a Linux Nerds

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

$ x=10 echo

its

value: $ echo The value of the variable x = $x The value of the variable x = 10

Note: The ‘-e‘ option in Linux acts as an interpretation of escape characters that are diagonal backwards

.

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

$ echo -e “Tecmint bis ba bcommunity bof bLinux bNerds” TecmintisacommunityofLinuxNerds

4. Using the ‘n‘ – New Line with Backspace Interpreter ‘-e‘ option treats the new line from where it is used.

$ echo -e “Tecmint nis na ncommunity nof nLinux nNerds” Tecmint is a Linux Nerds

5 community. Using the option ‘t‘ – horizontal tab with backspace interpreter ‘-e‘ to have horizontal tab spaces.

$ echo -e “Tecmint tis ta tcommunity tof tLinux tNerds” Tecmint is a Linux Nerds

6 community. How about using the new Line option ‘n’ and the horizontal tab ‘t’ simultaneously? $ echo -e “ntTecmint ntis nta ntcommunity ntof ntLinux ntNerds

” Tecmint is a Linux Nerds

7 community. Using the option ‘v‘ – vertical tab with backspace interpreter ‘-e‘ to have vertical tab spaces.

$ echo -e “vTecmint vis va vcommunity vof vLinux vNerds” Tecmint is a Linux Nerds

8 community. How about using the new Line option ‘n’ and the vertical tab ‘v’ simultaneously? $ echo -e “nvTecmint nvis nva nvcommunity nvof nvLinux nvNerds

” Tecmint is a community of Linux nerds

Note : We can duplicate the vertical tab, the horizontal tab and the new line spacing using the option twice or as many times as needed

.

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

$ echo -e “Tecmint ris a community of Linux Nerds” is a Linux Nerds

10 community. Using the ‘c‘ option – suppresses the new end line with the backspace interpreter ‘-e‘ to continue without emitting a new line.

$ echo -e “Tecmint is a community cof Linux Nerds” Tecmint is a community [email protected]:~$

11. Skip the echo of the new end line using the ‘-n‘ option.

$ echo -n “Tecmint is a Linux Nerds Community” Tecmint is a Linux Community [email protected]:~/Documents$

12. Using the option ‘a‘ – alert return with backspace interpreter ‘-e‘ to have the audible alert.

$ echo -e “Tecmint is a community of aLinux Nerds” Tecmint is a Linux Nerds community

Note: Be sure to check the volume key before shooting

.

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

$ echo * 103.odt 103.pdf 104.odt 104.pdf 105.odt 105.pdf 106.odt 106.pdf 107.odt 107.pdf 108a.odt 108.odt 108.pdf 109.odt 109.pdf 110b.odt 110.odt 110.pdf 111.odt 111.pdf 112.odt 112.pdf 113.odt linux-headers-3.16.0-customkernel_1_amd64.deb linux-image-3.16.0-customkernel_1_amd64.deb network.jpeg

14. Print files of a specific type. For example, suppose you want to print all the ‘.jpeg‘ files, use the following command.

$ echo *.jpeg network.jpeg

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

$ echo “Test page” > test page ## Verify content [email protected]:~$ cat testpage Test page echo

echo options

Description of options -n Do not print the new end line. -e allow the interpretation of backslash escapes. b backspace \ backslash n new line r carriage return t horizontal tab v vertical tab

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

Contact US