Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14293 > unrolled thread

Re: Redirect to variable

Started byGreg Wooledge <wooledg@eeg.ccf.org>
First post2018-07-03 08:53 -0400
Last post2018-07-03 08:53 -0400
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Redirect to variable Greg Wooledge <wooledg@eeg.ccf.org> - 2018-07-03 08:53 -0400

#14293 — Re: Redirect to variable

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2018-07-03 08:53 -0400
SubjectRe: Redirect to variable
Message-ID<mailman.3002.1530622426.1292.bug-bash@gnu.org>
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".

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web