user2268507
user2268507

Reputation:

Remove final '\n' from string variable

I'm trying to remove the final '\n' character from a string variable.

For example:

string
string
string
\n

should simply become:

string
string
string

A lot of the examples I have seen do this for a file but not a variable.

Any help would be greatly appreciated!

Upvotes: 1

Views: 172

Answers (2)

anubhava
anubhava

Reputation: 786091

In BASH you can do this to remove last newline:

eol=$'\n'
str="${s%$eol}"

Upvotes: 1

Robby Cornelissen
Robby Cornelissen

Reputation: 97331

You can use parameter expansion like this:

variable="${variable%$'\n'}"

Upvotes: 3

Related Questions