{"id":127156,"date":"2025-04-18T12:50:21","date_gmt":"2025-04-18T12:50:21","guid":{"rendered":"\/tutorials\/?p=127156"},"modified":"2025-04-27T19:13:57","modified_gmt":"2025-04-27T19:13:57","slug":"echo-command-linux","status":"publish","type":"post","link":"\/tutorials\/echo-command-linux","title":{"rendered":"How to use the echo command in Linux"},"content":{"rendered":"<p>The <strong>echo<\/strong> command is a helpful tool for tasks like shell scripting, output formatting, and debugging. With it, Linux users can print to their terminal, view system variables, display environment information, and format data.<\/p><p>echo is a great asset for developers who like to debug on the go and in the terminal, and it works consistently across Linux distributions. It&rsquo;s often paired with other commands, such as <strong>ls<\/strong> and <strong>pwd<\/strong>, to display file lists, paths, or context-specific messages during script execution.<\/p><p>In the following article, we&rsquo;ll explore echo command usage, syntax, and options, and we&rsquo;ll outline some practical examples.<\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-basic-syntax-of-the-echo-command\">Basic syntax of the echo command<\/h2><p>At its core, the syntax of the Linux echo command is as follows:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo [option(s)] [string(s)]<\/pre><p>In the above:<\/p><ul class=\"wp-block-list\">\n<li><strong>option<\/strong> can include flags, such as <strong>-n<\/strong> and <strong>-E<\/strong>, which are not compulsory. It can be one or more.<\/li>\n\n\n\n<li><strong>string<\/strong> is the text you want to print to the terminal. It can be one or more.<\/li>\n<\/ul><h3 class=\"wp-block-heading\" id=\"h-understanding-echo-command-options\">Understanding echo command options<\/h3><p>Options are flags you can add to your echo command to enrich your instructions and make them more specific. They format the data that will be printed to your terminal.<\/p><ul class=\"wp-block-list\">\n<li><strong>-n: <\/strong>Omits trailing newlines.<\/li>\n\n\n\n<li><strong>-e:<\/strong> Enables backslash escapes (like <strong>\\n<\/strong> and <strong>\\t<\/strong>).<\/li>\n\n\n\n<li><strong>-E:<\/strong> Disables backslash escapes (default behavior).<\/li>\n<\/ul><p>When running the echo command, the options should always go after echo and before the string.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"2048\" height=\"566\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/public\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-practical-echo-command-examples\">Practical echo command examples<\/h2><p>Let&rsquo;s take a look at specific Linux echo command use cases. These examples show how echo can be used for simple output, interactive scripts, formatting text, and integrating with other commands in real-world scenarios.<\/p><h3 class=\"wp-block-heading\" id=\"h-printing-a-string\">Printing a string<\/h3><p>We have already seen that to print a string with the echo command you can run:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo [string]<\/pre><p>For example, if you want to print a simple string, you can run the following on the command line:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo &ldquo;Testing this thing!&rdquo;<\/pre><p>Which will print your sentence to the terminal.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69df8d4dc06bb\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1460\" height=\"158\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-basic-syntax.jpg\/public\" alt=\"Simply add a string after the echo command to print it to your terminal.\" class=\"wp-image-127159\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-basic-syntax.jpg\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-basic-syntax.jpg\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-basic-syntax.jpg\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-basic-syntax.jpg\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-basic-syntax.jpg\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>You don&rsquo;t need to use quotation marks (&ldquo;&rdquo;) with the <strong>echo <\/strong>command, but you can if you are used to it when writing strings.<\/p><p>echo is often used in conjunction with other Linux commands. For example, used together with the <a href=\"\/tutorials\/ls-command\">ls command<\/a>, you can display the directory contents along with custom messages:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo \"Here are the all the files in your directory:\"; ls # Shows a list of your files in your current directory<\/pre><p>This will display:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Here are all the files in your directory:\n\nApplications&nbsp; &nbsp; YourPrivateFolder&nbsp; &nbsp; &nbsp; &nbsp;\n\nDesktop&nbsp; &nbsp; &nbsp; &nbsp; AnotherPrivateFolder&nbsp; &nbsp; &nbsp; &nbsp;\n\nDocuments&nbsp; &nbsp; &nbsp;\n\nDownloads<\/pre><h3 class=\"wp-block-heading\" id=\"h-using-n-to-remove-newline\">Using -n to remove newline<\/h3><p>As we previously mentioned, you can add a<strong> -n<\/strong> flag to your echo command to omit trailing newlines.<\/p><p>A common use case would involve a multi-step echo script that asks for user input. In such cases, you typically want the prompt to appear on the same line as the input field. Using <strong>echo -n<\/strong> helps you print the prompt without a newline.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n\necho -n \"Enter your username: \"\n\nread username\n\necho \"Hello, $username!\"<\/pre><p>Running the above script will return:<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69df8d4dc12cd\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1460\" height=\"134\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-remove-newline.jpg\/public\" alt=\"Using echo -n helps you print the prompt without a newline.\" class=\"wp-image-127160\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-remove-newline.jpg\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-remove-newline.jpg\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-remove-newline.jpg\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-remove-newline.jpg\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/04\/echo-command-linux-remove-newline.jpg\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-using-e-to-enable-backslash-escapes\">Using -e to enable backslash escapes<\/h3><p>As we have seen, when using the echo command,<strong> -e<\/strong> enables backslash escapes, like <strong>\\n<\/strong> (newline).<\/p><p>If you want to print one or more strings on multiple lines, you can run:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo -e \"See you later\\nalligator\"<\/pre><p>This will print:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">See you later\n\nalligator<\/pre><h3 class=\"wp-block-heading\" id=\"h-printing-environment-variables\">Printing environment variables<\/h3><p>You can assign your own value to a variable and then print it. To print the value of an environment variable, you can use the echo<strong> <\/strong>command followed by the dollar sign (<strong>$<\/strong>) and the variable name. For example, you can use this command to store temporary values like filenames, user input, counters, or results from other Linux commands.<\/p><p>Running the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">filename=\"report.txt\"\n\necho \"The file name is $filename\"<\/pre><p>Will print:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">The file name is report.txt<\/pre><h3 class=\"wp-block-heading\" id=\"h-combining-multiple-strings\">Combining multiple strings<\/h3><p>With the echo command, you can print multiple strings at a time. For example, running:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo \"See you later\" \"Alligator\"<\/pre><p>Will print the two strings one next to the other:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">See you later Alligator<\/pre><h3 class=\"wp-block-heading\" id=\"h-redirecting-output\">Redirecting output<\/h3><p>With the Linux echo command, you can redirect output to a text file instead of displaying it on the terminal by using the <strong>&gt;<\/strong> symbol. This can be useful for saving logs, messages, or command outputs into a text file for later use.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo \"Message: Some message here\" &gt; file.txt<\/pre><p>Here:<\/p><ol class=\"wp-block-list\">\n<li><strong>file.txt<\/strong> is created, if it doesn&rsquo;t already exist. Otherwise, it&rsquo;s overwritten.<\/li>\n\n\n\n<li>The string containing the message is written to <strong>file.txt<\/strong>.<\/li>\n\n\n\n<li>The string will now be inside the file, but nothing will be printed to the terminal.<\/li>\n<\/ol><p>You can also append text to an existing file instead of overwriting it by using the <strong>&gt;&gt;<\/strong> operator.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo \"Message: Another message!\" &gt;&gt; file.txt<\/pre><h3 class=\"wp-block-heading\" id=\"h-handling-special-characters-and-escape-sequences\">Handling special characters and escape sequences<\/h3><p>With the <strong>echo<\/strong> command, you can handle special characters and escape sequences with the <strong>-e<\/strong> option. This allows you to add formatting to the text output, such as newlines, horizontal tab spaces, or other special symbols.<\/p><p>Common escape sequences include:<\/p><ul class=\"wp-block-list\">\n<li><strong>\\n:<\/strong> Newline (it moves the output to the next line)<\/li>\n<\/ul><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo -e \"First line\\nSecond line\"<\/pre><p>Prints:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">First line\n\nSecond line<\/pre><ul class=\"wp-block-list\">\n<li><strong>\\t:<\/strong> Tab (it adds horizontal tab spaces)<\/li>\n<\/ul><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo -e \"Item\\tPrice\"<\/pre><p>Prints:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Item Price<\/pre><ul class=\"wp-block-list\">\n<li><strong>\\r: <\/strong>Carriage Return (it moves the cursor to the beginning of the line)<\/li>\n<\/ul><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo -e \"12345\\rABC\"<\/pre><p>Prints:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ABC45<\/pre><ul class=\"wp-block-list\">\n<li><strong>\\\\: <\/strong>Backslash (it inserts a literal backslash)<\/li>\n<\/ul><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo -e \"Path: C:\\\\Users\\\\Name\"<\/pre><p>Prints:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Path: C:\\Users\\Name<\/pre><ul class=\"wp-block-list\">\n<li><strong>\\b:<\/strong> Backspace (it moves the cursor one character backward)<\/li>\n<\/ul><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo -e \"helloo\\b!\"<\/pre><p>Prints:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">hello!<\/pre><h3 class=\"wp-block-heading\" id=\"h-disabling-backslash-escapes\">Disabling backslash escapes<\/h3><p>In the echo command, using the <strong>-E<\/strong> option allows you to disable the backslash escape. Imagine you are writing a script that outputs user-generated content, where you want the content to be printed out without any special interpretation of escape sequences. The standard approach would be to interpret them as their intended meaning:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">message=\"This\\nis user-generated info!\"\n\necho -e \"$message\"<\/pre><p>Without <strong>-E <\/strong>but using<strong> -e<\/strong>, <strong>\\n<\/strong> is interpreted as a newline.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">This\n\nis user-generated info!<\/pre><p>However, if we do:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">message=\"This\\nis user generated info!\"\n\necho -E \"$message\"<\/pre><p><strong>\\n<\/strong> is interpreted literally, and there is no newline.&nbsp;<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">This\\nis user generated info!<\/pre><p>This can be useful in situations where escape sequences are part of the intended output, like log outputs, code snippets, JSON strings, regex, or configuration files.<\/p><p>For example, when outputting JSON, you want to make sure to keep it as-is:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">json='{\"message\": \"Backup completed\\\\nNext backup at 03:00\"}'\n\necho -E \"$json\"\n\n{\"message\": \"Backup completed\\nNext backup at 03:00\"}<\/pre><p>Because JSON is a strict data format, it is crucial to keep it as-is, with correct escape characters and structure. Changing the formatting can break its validity, and anything that tries to read it &ndash; apps, APIs, parsers, or linters &ndash; will throw an error or behave unpredictably.<\/p><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>The echo command in Linux is an extremely versatile tool for outputting strings, manipulating text, and working with variables. In this article, we explored how different options, such as <strong>-n<\/strong>, <strong>-e<\/strong>, and <strong>-E<\/strong>, can handle escape characters, influence formatting, and manipulate output in a variety of scripting scenarios.<\/p><p>echo is a simple command that, when combined with other tools, proves increasingly useful across different contexts, from writing logs and generating JSON to building interactive scripts and managing variables. Whatever your sector, you can take full advantage of echo&rsquo;s capabilities by experimenting with it, and making it an essential tool in your Linux toolbox.<\/p><h2 class=\"wp-block-heading\" id=\"h-the-echo-command-in-linux-faq\">The echo command in Linux FAQ<\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1744980264603\"><h3 class=\"schema-faq-question\">What is the echo command in Linux?<\/h3> <p class=\"schema-faq-answer\">The echo command in Linux is a valuable tool for interacting with users and debugging scripts. It&rsquo;s employed by developers to print text or variables to the terminal. Common tasks include displaying messages, formatting text, or outputting the value of variables in shell scripts and command-line operations.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1744980268961\"><h3 class=\"schema-faq-question\">How do I use the echo command?<\/h3> <p class=\"schema-faq-answer\">You can run the echo command by typing <strong>echo [options] [string] <\/strong>in the terminal. For example, echo &ldquo;Hello!&rdquo; will print the text to the terminal. You can add options such as&nbsp; <strong>-n<\/strong>, <strong>-e<\/strong>, and <strong>-E<\/strong> to manipulate your text, and use the dollar sign (<strong>$<\/strong>) to assign variables.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1744980274936\"><h3 class=\"schema-faq-question\">What is the difference between echo and cat commands?<\/h3> <p class=\"schema-faq-answer\">While the echo command prints text or variables, the<a href=\"\/tutorials\/linux-cat-command-tutorial-and-examples\/\"> <strong>cat<\/strong> command<\/a> is used to show the contents of files. echo is typically used for outputting strings, while cat is used for reading files.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>The echo command is a helpful tool for tasks like shell scripting, output formatting, and debugging. With it, Linux users [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/echo-command-linux\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":536,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"The echo command in Linux with examples","rank_math_description":"The echo command lets you print to the terminal, view system variables, display environment information, and format data. Read more about specific syntax and examples.","rank_math_focus_keyword":"echo command linux","footnotes":""},"categories":[22648,22644],"tags":[],"class_list":["post-127156","post","type-post","status-publish","format-standard","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/echo-command-linux","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/echo-command-linux","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/echo-command-linux","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/echo-command-linux","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/echo-command-linux","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/echo-command-linux","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/echo-command-linux","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/echo-command-linux","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/127156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/536"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=127156"}],"version-history":[{"count":5,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/127156\/revisions"}],"predecessor-version":[{"id":127439,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/127156\/revisions\/127439"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=127156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=127156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=127156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}