Table of contents
1.
Introduction
2.
Echo a New Line in Linux
2.1.
Displaying New Line with -r flag of Echo command
2.2.
Linux
2.3.
Echo a Variable containing a New Line
2.4.
Linux
2.5.
Using ‘$’ Character 
2.6.
Linux
2.7.
Using Echo with Echo to print something with New Line
2.8.
Linux
2.9.
Using printf command in Bash shell
2.10.
Linux
3.
Frequently Asked Questions
3.1.
What is the echo command in Linux?
3.2.
Can we use Echo to print multiple lines of text?
3.3.
What is the difference between printf and echo?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

How to Echo a New Line in Linux

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Just like iOS, Windows, Linux is also an Operating System. In fact, the Andriod operating system, which is one of the most popular platforms, is powered by the Linux operating system. Organizations and individuals use Linux for their servers as it is flexible and secure, and we can receive amazing support from a large community of users. 

How to echo a new line in linux

We can easily install Linux in our system, or there are computers such as System76 or Purism with already installed Lunix. In this article, we are going to see how to echo a new line in Linux. So stay till the end, to learn the most of it!

Echo a New Line in Linux

Echo new line command in Linux is very essential for showing formatted text output. So let's start with various methods to echo a new line in Linux, allowing effective output formatting.

Displaying New Line with -r flag of Echo command

While working with the echo command in Linux, we can use the -e flag to display a new line. It is a recommended option for echoing a new line. It enables the interpretation of escape sequences, which makes it easier to format our results. The escape sequence used for a new line is the \n character. The command is as follows:

  • Linux

Linux

echo -e "Welcome to Coding Ninjas.\nThis is the second line."


Output:

output

Echo a Variable containing a New Line

Linux operating system gives us very effective ways to handle cases where we need to echo the contents of a variable containing newline characters. The command for this is as follows:

  • Linux

Linux

$ new_var="Welcome to Coding Ninjas.
This is the second line."
echo -e "$new_var"


Output:

output

In the above example, we have assigned a multi-lined string to the variable new_var. By providing the variable within double quotes and using the -e flag with the echo command, the shell will interpret the newline character stored within the variable. Due to this, we have got the above output.

Using ‘$’ Character 

We can use the '$' character instead of the -e flag with the echo command in Linux to echo a new line. It makes the process simple and easy when the -e flag is not preferred or unavailable. 

  • Linux

Linux

echo $"Welcome to Coding Ninjas.
This is the second line."


Output:

output

The '$' gives the same result as the -e flag; the only difference is in syntax. However, this is not as flexible as the -e flag; it only interprets escape sequence and doesn't support other formatting options.

Using Echo with Echo to print something with New Line

In some situations, we may need to echo the result of an echo command for printing text that includes a new line. The command to use nesting echo is as follows:

  • Linux

Linux

$ echo "$(echo "Welcome to Coding Ninjas." && echo "This is the second line.")"


Output:

output

In the above example, we have used the nesting echo command to create the desired line of text, and the outer echo command is responsible for printing the whole output. This method is not considered as the most efficient approach in terms of performance. As nesting multiple commands can lead to unnecessary overheads.

Using printf command in Bash shell

The print command in the Bash shell is considered an effective method for printing new lines. Its formatting and flexibility options make it powerful for controlling the output layout. The example is as follows:

  • Linux

Linux

$ printf "This is the first line.\nThis is the second line.\n"


Output:

output

In the above example, we have used printf instead of the echo command.

Frequently Asked Questions

What is the echo command in Linux?

In Linux, the echo command is the command that prints text to the standard output. This command is one of the most commonly used commands in Linux.

Can we use Echo to print multiple lines of text?

Yes, we can use echo to print multiple lines of text using \n escape character. Example:

echo “Welcome to CN”\n This is the second line”

What is the difference between printf and echo?

Both printf and echo commands are used to print text to the standard output. The echo command is a simple command that does not support many formatting options, whereas the printf command is a powerful command that supports a wider range of formatting options.

Conclusion

In this article, we have discussed how to echo a new line in Linux. We have seen multiple different methods by which we can echo a new line in Linux. We hope this blog has helped you enhance your knowledge of how to echo a new line in Linux. If you want to learn more, then check out our articles:


Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must have a look at the problemsinterview experiences, and interview bundles for placement preparations.

Nevertheless, consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass