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

Channel: Linux
Abstract: How to use \n in shell script to print new line. Issues – Echo newline in bash shell prints literal \n but not new line. Printing literal ‘\n’ in nest

Questions – How can I print a newline as \n in bash shell? How to use \n in shell script to print new line.
Issues – Echo newline in bash shell prints literal \n but not new line. Printing literal ‘\n’ in 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 to with echo command. Read 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 newline character in bash shell scripts. Use one of 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.

Ref From: tecadmin

Related articles