{"id":1770,"date":"2022-02-15T18:21:07","date_gmt":"2022-02-15T12:51:07","guid":{"rendered":"https:\/\/smarttech101.com\/?p=1770"},"modified":"2024-02-22T21:17:32","modified_gmt":"2024-02-22T15:47:32","slug":"echo-with-examples","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/echo-with-examples\/","title":{"rendered":"Echo Command in Linux With Practical Examples"},"content":{"rendered":"\n
In this article, I will talk about echo command in Linux with its examples and basic applications. I will also give a little information about special sequences, and quotes<\/strong> in Linux shell.<\/p>\n\n\n\n Note<\/strong>: Some shells have their own echo commands which are little different from the main echo command. This article is for bash’s echo<\/strong>. For other shells, there is not much difference. At the same time, I will also try my best to include these differences. And one more thing to note is that most of these commands should also work on any Unix-based system.<\/p>\n\n\n\n Syntax:<\/p>\n\n\n\n In the above syntax, three dots ( The simplest example:<\/p>\n\n\n\n In the above example, there is only one argument written under the quotes. If I remove the quotes, It becomes two separate arguments but the results would be the same. For example,<\/p>\n\n\n\n The above example might tempt you to think that anything after the echo is printed out. But that is not true. All the POSIX compliant shells terminate any command at semicolon ( To prevent this termination at the semicolon ( Given all the above issues associated with semicolons, backslashes, and quotes, you might get confused sometimes. This is true if you are a beginner. My suggestions would be to always use double quotes with the echo.<\/p>\n\n\n\n One more thing to note is that the meaning of ‘arg’ is divers, it can be a parameter with In all of the above examples, as you can see, echo is inserting the newline ( Example:<\/p>\n\n\n\n The new line ( The escape sequences in echo start with the backlash. Most of these escape sequences, very often, are valid in other commands as well (ex – printf<\/a>, gawk, python, etc.).<\/p>\n\n\n\n By default, these escape sequences are disabled in bash<\/strong> as shown below:<\/p>\n\n\n\n To enable the interpretation of escape sequences, you need to use the flag -e<\/strong>:<\/p>\n\n\n\n Note<\/strong>: In dash<\/strong> shell, these escape sequences are disabled by default. On the other hand, in zsh<\/strong> shell, they are enabled. To disable it, use the flag After enabling the escape sequences, I will describe the types of escape sequences one by one with examples.<\/p>\n\n\n\n The backspace sequence deletes one character ahead of it. For example,<\/p>\n\n\n\n It puts the cursor at the beginning of the text. In the example given below, ‘NNNN’, then, replaces ‘firs’.<\/p>\n\n\n\n Example:<\/p>\n\n\n\n The horizontal tab is what you see when you press the tab button in any text editor.<\/p>\n\n\n\n Example:<\/p>\n\n\n\n This is best understandable by the following example:<\/p>\n\n\n\n This is also called a bell sequence. It does not print anything, instead, you will hear a beep sound.<\/p>\n\n\n\n It suppresses further output i.e. as soon as echo encounters Note: In the command gawk, ‘\\c’ means a different thing.<\/p>\n\n\n\n In order to print a single backslash, you will need to use double backslashes. Example:<\/p>\n\n\n\n Technically, it directs the printer to advance down to the next “page”. But in the terminal, you will see the output similar to the vertical tab. I have not found its use yet. If you have any, please comment below.<\/p>\n\n\n\n Echo can also print out any character corresponding to the octal number ‘nnn’. Here the number ‘nnn’ can have one to three digits. For example, ‘013’ and ’13’ correspond to a vertical tab, ‘047’ and ’47’ to a single quote, ‘042’ and ’42’ to a double quote, and so on. Many of these characters and corresponding octal numbers can be found on this link.<\/a><\/p>\n\n\n\n Here, echo prints the character corresponding to one or two digits hexadecimal number. For example, ‘0b’ or ‘b’ is the same as a vertical tab, ’26’ is the same as a single quote, and ’22’ is the same as a double quote, and so on. Other matches between hexadecimal numbers and characters <\/a>can be found in the same hyperlink given above.<\/p>\n\n\n\n Examples:<\/p>\n\n\n\n Here, HHHH in When echo encounters these escape sequences, it prints the corresponding Unicode (ISO\/IEC 10646) character.<\/p>\n\n\n\nTable of Contents<\/h2>\n\n\n\n
\n
\n
Basic Applications of Echo Command in Linux<\/h2>\n\n\n\n
echo [-neE] [arg ...]<\/code><\/pre>\n\n\n\n
...<\/code>) means you can have as many
arg<\/code> as possible. The execution of the above command will print all the ‘args’ separated by space.<\/p>\n\n\n\n
[ajay@lenovo ~]$ echo \"hello world\"\nOutput:\nhello world<\/code><\/pre>\n\n\n\n
[ajay@lenovo ~]$ echo hello world\nOutput:\nhello world<\/code><\/pre>\n\n\n\n
;<\/code>). So, echo can access argument only up to the semicolon (
;<\/code>), after that, the shell treats all the world(s) as a separate command(s). For example given below, our shell tries to execute the command called ‘world’, but it cannot find that command and hence it throws the error:<\/p>\n\n\n\n
[ajay@lenovo ~]$ echo hello world; world\nOutput:\nhello world\nbash: world: command not found<\/code><\/pre>\n\n\n\n
;<\/code>), prepend the semicolon with a backslash as in the following example:<\/p>\n\n\n\n
[ajay@lenovo ~]$ echo hello world\\; world\nOutput:\nhello world; world<\/code><\/pre>\n\n\n\n
$<\/code> (ex –
$USER<\/code>), or an even command (ex –
$(date)<\/code>), or their mix-ups as shown below:<\/p>\n\n\n\n
[ajay@lenovo ~]$ echo \"hello $USER\"\nOutput:\nhello ajay\n[ajay@lenovo ~]$ echo \"today's date is $(date)\"\nOutput:\ntoday's date is Tue Feb 15 12:35:40 AM IST 2022<\/code><\/pre>\n\n\n\n
How to Prevent Echo From Appending the Newline (\\n)<\/h2>\n\n\n\n
\\n<\/code>) at the end. To remove that, we need to use the flag
-n<\/code>.<\/p>\n\n\n\n
[ajay@lenovo ~]$ echo -n \"line 1\"; echo -n \"line 2\"\nOutput:\nline 1line 2[ajay@lenovo ~]$ <\/code><\/pre>\n\n\n\n
\\n<\/code>) is an escape sequence. There are others as well as shown under the next heading.<\/p>\n\n\n\n
Escape Sequences in Echo Command in Linux<\/h2>\n\n\n\n
[ajay@lenovo ~]$ echo \"hello\\nworld\"\nOutput:\nhello\\nworld<\/code><\/pre>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"hello\\nworld\"\nOutput:\nhello\nworld<\/code><\/pre>\n\n\n\n
-E<\/code>.<\/p>\n\n\n\n
Backspace (\\b) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"first line\\bsecond line\"\nOutput:\nfirst linsecond line<\/code><\/pre>\n\n\n\n
Carriage Return (\\r) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"first line \\rNNNN\"\nOutput:\nNNNNt line \n[ajay@lenovo ~]$ <\/code><\/pre>\n\n\n\n
Horizontal Tab (\\t) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"word1 \\tword2\" \nOutput:\nword1 word2<\/code><\/pre>\n\n\n\n
Vertical Tab (\\v) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"word1 \\vword2\" \nOutput:\nword1 \n word2<\/code><\/pre>\n\n\n\n
Alert (\\a) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"hello world \\a\"\nOutput:\nhello world <\/code><\/pre>\n\n\n\n
\\c in Echo<\/h3>\n\n\n\n
\\c<\/code>, echo does not print out anything.<\/p>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"hello\\c world\" \nOutput:\nhello[ajay@lenovo ~]$ <\/code><\/pre>\n\n\n\n
Literal Backslash (\\\\) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"hello \\\\ world\"\nOutput:\nhello \\ world<\/code><\/pre>\n\n\n\n
Formfeed (\\f) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"hello \\f world\"\nOutput:\nhello \n world<\/code><\/pre>\n\n\n\n
Octal Number Based Sequence (\\0nnn) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"hello \\0042world\\0042\"\nOutput:\nhello \"world\"\n[ajay@lenovo ~]$ echo -e \"hello \\042world\\042\"\nOutput:\nhello \"world\"<\/code><\/pre>\n\n\n\n
Hexadecimal Number Based Sequence (\\xhh) in Echo<\/h3>\n\n\n\n
[ajay@lenovo ~]$ echo -e \"hello \\x22world\\x22\"\nOutput:\nhello \"world\"\n[ajay@lenovo ~]$ echo -e \"hello \\xb world\"\nOutput:\nhello \n world<\/code><\/pre>\n\n\n\n
Escape Sequences \\uhhhh and \\Uhhhhhhhh for Unicode Characters in Echo<\/h3>\n\n\n\n
\\uHHHH<\/code> is a hexadecimal number from one to four digits. Similarly, HHHHHHHH in
\\uHHHHHHHH<\/code> is one to eight digits hexadecimal number.<\/p>\n\n\n\n