The bash echo command is used to print some text into the terminal or command-line interface. The echo command print specified text or variables in different ways. But in some cases, we may want to print a new line with the echo command.
New Line In Bash
Before trying to print a new line in the bash echo command we should explain what is a new line. A newline is used to specify the end of the line and jump to the next line. The end of the line is expressed with \n characters in Linux and Unix systems. The end of the line is not displayed by the text editors and related tools.
Print New Line with echo In Bash
The echo command is used to print provided text, data or variable into the standard output which is generally the terminal or console. By default the echo command do not interprets the “\n” as the new line. But by using the -e option the interpretation of the backslahes escapes can be enabled which will also interpret the new line characters.
echo -e "Hello\nLinux\nTect"
Hello Linux Tect
Alternatively the single quotos can be also used to print new line like below.
echo -e 'Hello\nLinux\nTect'
The echo command can also print newlines in a variable. In the following example, we create a variable named sentence which contains multiple ends of lines or new lines
sentence="Hello\nLinux\nTect" echo -e $sentence
Print New Line with Multiple Statements
Another way to print a new line with the echo command is to use multiple statements. The bash adds a new line after every statement automatically. Statements are delimited with the ; character in bash.
echo Hello; echo Linux; echo Tect;
Print New Line with $ Sign
An alternative way to print a new line in bash with the echo command is using the $ sign. The text is provided without any single or double quotes and $ sign added before the ‘\n’ new line. Also, we can see that the new line characters are provided inside single quotes.
Print New Line with printf Command
Even the echo command can be used to print a new line there are useful alternatives like the printf command. The printf command can be used to print a new line for the specified text, output, or variable too.