help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Removing newline \n embedded by regex


From: Greg Wooledge
Subject: Re: Removing newline \n embedded by regex
Date: Mon, 6 Sep 2021 23:02:47 -0400

On Tue, Sep 07, 2021 at 05:50:34AM +0300, Tapani Tarvainen wrote:
> On Tue, Sep 07, 2021 at 02:35:59AM +0000, Budi (budikusasi@gmail.com) wrote:
> 
> > How do we remove newline \n embedded on end of a string by use of bash regex
> 
> var="${var%
> }"
> 
> or
> 
> var="${var%'$\n'}"
> 
> or
> 
> var="${var%'$\012'}"
> 
> or
> 
> var="${var%'$\x0A'}"

Note: none of these involves the use of a "regex".  Parameter expansion
with the % modifier involves a glob-style pattern match.  And in this
particular case, the pattern is actually a single-character constant
string.

A better question is: why does your string have a newline at the end of
it?  How did you get this value into the string in the first place?

If you used read, the delimiter (newline by default) is automatically
stripped for you.

If you used mapfile (readarray), you can use the -t option to strip
the trailing newlines from each input line.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]