FreeKB - Remove new lines from a variable in Linux Bash
Remove new lines from a variable in Linux Bash

Home > Search


Let's say you have a varialbe (foo in this example) that contains new lines.

foo="line 1
line 2
line 3"

 

To remove all of the new lines.

foo=${foo||$'\n'|}

 

To remove only the very last new line.

foo=${foo%$'\n'}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments