Bash (Scripting) - remove new lines from a variable

by
Jeremy Canfield |
Updated: March 08 2020
| Bash (Scripting) articles
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'}
Did you find this article helpful?
If so, consider buying me a coffee over at