help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Transform strings with special characters so that the st


From: Greg Wooledge
Subject: Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?
Date: Mon, 30 Mar 2015 11:41:04 -0400
User-agent: Mutt/1.4.2.3i

On Mon, Mar 30, 2015 at 03:39:58PM +0100, Stephane Chazelas wrote:
> 2015-03-30 09:47:16 -0400, Greg Wooledge:
> > In #bash we (de facto) agreed on this version:
> > 
> > var=$(cmd; printf x) var=${var%x}
> > 
> > Which is basically the same as yours 
> 
> That doesn't trim the one trailing newline you usually want to
> trim (which was the point of my two var=${var%?})

I don't know what your use case is, but in ours, we typically want to
preserve the exact output of the command.  We don't arbitrarily want
to strip just one newline and leave the others.

Most often we use this when trying to read the contents of a file into
a variable:

content=$(cat file; printf x) content=${content%x}

which is sad, because it means we don't get to use $(< file) at all.

> and that fails
> to preserve the exit status of cmd though.

That's true.



reply via email to

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