How to Insert a New Line Character in Linux Shell Script Output ?


In bash, a newline refers to a line's end and a text's beginning. In Linux/Unix operating systems, the new line character is represented as "
," which instructs the terminal to move the cursor to the beginning of the next line. Many text editors don't show it by default. Inserting a new line character has several important reasons −

  • It helps format the output to be understood and readable.

  • Separating the output into sections makes it easier to locate specific information.

  • The newline is also used as a line break that identifies the end of a line in a script to mark the beginning of a new line.

  • Additionally, you can use it to go to the next line.

Sometimes you need to insert a newline character to format and modify your output. In this article, we will see how to insert a new line character in Linux shell script output with the help of some Linux commands.

How to Insert a New Line Character in Linux Shell Script Output

If you are a bash user and want to insert '
' in the script, you can do it through various commands. Here we will use the 'echo' and 'printf' commands to insert a new line character.

The echo Command

The echo Linux command is commonly used for inserting standard output. Using this command, you can print a string −

~$: echo "Tutorial
Points" Tutorial
Points

As the above output displays, it has taken
as a character. Echo treats
as a regular part of the input string by default. So here, you can add the -e option to enable the echo command and interrupt the new line character. Whether we use -e or not, by default, echo adds
at the end of each sentence.

~$: echo -e "Tutorial
Points" Tutorial Points

The -e option does not work in all versions and systems; some echo versions print -e as output. So echo is only portable if escape and flags are omitted, which is useless. To solve this, you can apply
using printf.

Multiple Echo Commands

This approach uses multiple echo commands to print the new line character instead of one. By default, echo prints the given string and inserts newline characters.

~$: echo Tutorial; echo Points
Tutorial
Points

A semicolon (;) separates each echo command in the above example (;).

'$' with the Echo Command

The dollar symbol ($) is called 'expansion' in a shell script. You can use the dollar ($) sign to insert a newline character with the echo command. It may seem complicated to use this method. Please make sure that you keep these things in mind while using this approach −

  • It is always necessary to use the "$" sign before each newline character "
    ."

  • Newline characters "
    " must be enclosed in single quotes. Many users have it in double quotes, which leads to errors.

~$: echo Tutorial'
'Points Tutorial Points

In the above command, you will see that, in this case, the expansion character acts as a temporary value.

The Printf Command

As its name defines, the printf command is used to print something. You can use this command as an alternative to the echo command. Apart from specifying a format string, this command allows you to control the output in more detail.

~$: printf "Tutorial
Points
" Tutorial Points

Like the echo command in printf, an option or flag is not required to interpret
in a sentence. The string only needs to be written between the two quotation marks. Thus the newline character is applied by default. Printf works for all systems, but in terms of performance, it is slower than the echo (built-in shell) command.

Conclusion

So this was all about the simple commands you can use to insert a new line character
in the output of Linux. This has revolved around adding
to the output with the help of 'echo' and 'printf' commands. There are many other approaches to the echo command by which you can insert
. You can insert several new characters simultaneously instead of one using multiple echo commands. You can do the same using the $ sign with the echo command.

Moreover, we looked at printing a character using the 'printf' command. You do not need to use any flag with this command as you do with the echo command. Both commands are considered reliable for inserting backslash-escaped characters, and each has its unique behavior.

Updated on: 18-May-2023

554 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements