Path: csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Greg Wooledge Newsgroups: gnu.bash.bug Subject: Re: Redirect to variable Date: Tue, 3 Jul 2018 08:53:11 -0400 Lines: 29 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: usenet.stanford.edu 1530622426 25884 208.118.235.17 (3 Jul 2018 12:53:46 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash@gnu.org To: Robert Durkacz Envelope-to: bug-bash@gnu.org Mail-Followup-To: Robert Durkacz , bug-bash@gnu.org Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 139.137.100.1 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:14293 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".