The echo command is simple, except when it isn't. Here's a look at the basic command along with some of the more challenging things it can do. Credit: Getty Images The echo command (a bash built-in) is one of the very basic commands on Linux. As with ls and pwd, you can’t sit on the command line very long without using it. At the same time, echo has quite a few uses that many of us never take advantage of. So, this post looks into the many ways you can use this command. What is the echo command on Linux? Basically, echo is a command that will display any text that you ask it to display. However, when you type “echo hello”, the echo command isn’t only spitting out those five letters, it’s actually sending out six characters – the last one being a linefeed. Let’s look at a couple of commands that make this obvious. First, using echo with no arguments does this: $ echo $ Notice that an empty line is displayed before the prompt comes back. If you redirect the command’s output to the od -bc command, it’s easy to confirm that the “invisible” output is simply a linefeed – that n or octal 012 in the output below. $ echo | od -bc 0000000 012 n 0000001 That n is the reason that the echo command can also be used to add a linefeed to the end of a file that, for some reason, lacks one. Here’s a file that lacks a linefeed followed by the command that adds one. $ od -bc nolinefeed 0000000 150 145 154 154 157 h e l l o 0000005 $ echo >> nolinefeed $ od -bc nolinefeed 0000000 150 145 154 154 157 012 h e l l o n 0000006 od -bc The od -bc command in the above example displays the content of files in both octal and character format. The output above shows that a linefeed was added to the end of the file. The od -bc command provides a very useful way to examine the contents of a file, especially when using the cat command doesn’t show you everything you need to see. > vs >> One important thing to pay attention to when using the echo command is that > and >> work with files differently. The > will overwrite the content of a file, while >> will append to it. echo -n The echo -n command will omit the linefeed from its output. This is often used in scripts when prompting the user to provide an answer to some question. This works best when the prompt and the space where the answer will be entered are on the same line. For example, if the user should provide the name of the file to be processed by the script, we’re likely to see lines like these: echo -n "file name> " read file Whoever runs the script will be prompted to enter a file name and will type the file name as shown here: file name> datafile Emptying Linux files The echo command can also be used to empty files. While it’s more common to use a command such as cat /dev/null > filename, the command echo -n > filename works as well. Both commands replace the content of a file with … nothing! To turn this command into an alias, use a command like this or add it to your .bashrc file. $ alias empty='echo -n > ' After setting up the alias, you can just type a command such as “empty myfile”, and the file will no longer have content. Keep in mind that using >> in place of > would leave the file intact – basically adding nothing to the end of it. Using echo -e The echo command’s -e option (enable interpretation of backslash escapes) interprets a backslash followed by some other character (e.g., b) in a way that alters the output. For example, inserting instances of b in a string will cause the characters preceding b to be removed (backspaced over). Here’s an example: $ motto="When blife bhands byou blemons, bmake blemonade" $ echo -e $motto Whenlifehandsyoulemons,makelemonade If the text includes linefeed (n) characters instead of backspace characters, the command turns the text into multiple lines instead – basically creating newlines. $ motto2='When nlife nhands nyou nlemons, nmake nlemonade' $ echo -e $motto2 When life hands you lemons, make lemonade Using c will suppress newline characters and, thus, truncate the output. $ motto3="When life hands you clemons, make lemonade" $ echo -e $motto3 When life hands you $ Using t inserts tabs into the output. $ echo $motto4 When life hands you tlemons, tmake lemonade $ echo -e $motto4 When life hands you lemons, make lemonade The man page for the echo command will show you all of the sequences that you can use with the -e option. backslash a alert (BEL) b backspace c produce no further output e escape f form feed n new line r carriage return t horizontal tab v vertical tab Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe