print new line character in Unix / Linux

This is quick post on print new line character in Unix / Linux. Here, we will share two methods of print / echo new line character. Generally , when we do bash/shell scripting most of the time we require this.

Let’s start this essential basic of bash/shell scripting.

echo command : new line character

In this section , we are using echo command to print new line character. Echo command can be used in various ways as per the requirement. Through echo command, you can also find out success status of last command run.

To print new line character by echo command use the given below format

The backslash character n should be used with -e option.
-e = For enabling interpretation of backslash escape
-n = Do not output the trailing newline
n = For new line [This is backslash character which print to new line]

Example 1 , with -e and n .

echo -e "Write some wordnNow printed new line"

Example 2, with -e , -n and n . You may have notice -en is written in this example. The -e and -n together can be written as -en .

echo -en "Write some wordnNow printed new line"

You may be thinking, what is the difference in these two examples. In Example 1 there is trailing new line whereas in Example 2 you will not find trailing new line due to use of -n with echo command . See the below given screenshot for clear understanding.

echo command

printf command : new line character

This is another alternate method to print new line character by using printf command. By default, on using only backslash character n , it will not print trailing new line (Just as we have seen in echo command Example 2)

To print new line character by printf command use the given below format

Printf command is used for format and print data. your shell may have its own version of printf .
Use the n option along with printf command.

n = For new line [This is backslash character which print to new line]

printf "Write some wordnNow printed new line"

Given below is screenshot of above example.

printf command

Note : It is good to know both method, because you may experience undesirable output depending upon the bash / command version. I personally prefer printf command.