Skip to content

How to Use Newline character ( \n ) in Bash Shell

Echo newline in bash shell prints literal n but not newline. Printing literal 'n' in nested print new line in bash scripts.How can I print a newline as n in bash shell?

On this page

Questions – How can I print a newline as \n in bash shell? How to use \n in a shell script to print new line.
Issues – Echo newline in bash shell prints literal \n but not new line. Printing literal '\n' in a nested print new line in bash scripts.

Command:


Use the following command to print newline using \n in bash shell scripting. You have other options with the echo command. Read the complete tutorial.

printf “first line\nsecond line\n”


Uses of \n in Bash

\n (Line Feed) is used as a newline character for Unix-based systems. Below is a simple example to use a newline characters in bash shell scripts. Use one of the followings examples

String in double quote:

echo -e "This is First Line \nThis is Second Line"

String in single quote:

echo -e 'This is First Line \nThis is Second Line'

String with $ prefix:

echo $'This is First Line \nThis is Second Line'

Using printf command:

printf "This is First Line \nThis is Second Line"

You may also like our bash scripting tutorial.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top