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

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!