[Top][All Lists]

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

Re: Redirect to variable

From: Greg Wooledge
Subject: Re: Redirect to variable
Date: Tue, 3 Jul 2018 08:53:11 -0400
User-agent: NeoMutt/20170113 (1.7.2)

On Tue, Jul 03, 2018 at 10:43:31PM +1000, Robert Durkacz wrote:
> On 5/21/18 Chet Ramey wrote:
> > What you're asking for is syntactic sugar for:
> > some-command > temp-file
> > echo '#' >> temp-file
> > variablename=$(< temp-file)
> > rm -f temp-file
> > variablename=${variablename%?}
> > I would look at a sample implementation, possibly using mmap, if someone
> did one.
> Could someone please explain the reason for inserting and removing the #
> character. It is as if to ensure temp-file is non-empty but it seems to me
> it would work anyway.

Command substitutions $(...) and `...` strip all trailing newline
characters from the output of the command.  So you don't get exactly
the output of the command.

In order to preserve the exact output, you need to append something
after the (potential) final newline(s), and then remove it afterward.
This works around the command substitution's newline-chomping feature.

Welcome to shell "programming".

reply via email to

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