Bootstrap FreeKB - Bash (Scripting) - remove new lines from a variable
Bash (Scripting) - remove new lines from a variable

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


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