How to add NewLine Character in Bash Script String


You can make use of the \n character to enter a new-line character in a bash script string. But you need to be mindful of what platform you are working on with the script. On Windows, you may need to use \r\n or rather choose $'\n' to make it platform independent.


PlatformNewline CharacterExample
Mac\necho -e "\nHello!\nHow are you?"
Windows\r\necho -e "\r\nHello!\r\nHow are you?"
Linux\necho -e "\nHello!\nHow are you?"
Works for All$'\n'echo -e "Hello!"$'\n'"How are you?"
Adding NewLine Character in Bash Script String

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap