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: Stephane Chazelas
Subject: Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?
Date: Mon, 30 Mar 2015 16:57:47 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2015-03-30 11:41:04 -0400, Greg Wooledge:
> 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.

Typical usage would be:

dir=$(dirname -- "$file")
base=$(dirname -- "$file")
link=$(readlink -- "$file")
string=$(expr "x$string" : "x\(.*\)whatever")
dir=$(pwd -P)

Or any command that returns a string on stdout where they add
their own one newline character to the value actually  returned.

> 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.
[...]

Note that zsh has a $mapfile[$file] for that (in a module).

-- 
Stephane




reply via email to

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