[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Trailing newlines disappear
From: |
Greg Wooledge |
Subject: |
Re: Trailing newlines disappear |
Date: |
Thu, 8 Jun 2017 13:42:39 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Fri, Jun 09, 2017 at 12:38:19AM +0700, PePa wrote:
> I would think this is a bug:
> 4.3.48(1)-release> printf "q\n\n\n" >w
> 4.3.48(1)-release> printf "$(cat w)"
> q
> 4.3.48(1)-release>
>
> Is there some workaround to somehow preserve the trailing newlines?
It's not a bug. This is how command substitution has worked since
the original Bourne shell.
The workaround is to put something inside the command substitution,
so that the newlines aren't trailing any more, and then strip it away
afterward:
foo=$(cat "$file"; printf x) foo=${foo%x}
This also means that you can't use the $(< "$file") shortcut any more,
if you need to preserve the full contents of a file.
- Trailing newlines disappear, PePa, 2017/06/08
- Re: Trailing newlines disappear,
Greg Wooledge <=
- Re: Trailing newlines disappear, Peter & Kelly Passchier, 2017/06/08
- Re: Trailing newlines disappear, PePa, 2017/06/08
- Re: Trailing newlines disappear, Geir Hauge, 2017/06/08
- Re: Trailing newlines disappear, Peter & Kelly Passchier, 2017/06/08
- Re: Trailing newlines disappear, Greg Wooledge, 2017/06/08
- Re: Trailing newlines disappear, Eduardo Bustamante, 2017/06/08
- Re: Trailing newlines disappear, Peter & Kelly Passchier, 2017/06/08
- Re: Trailing newlines disappear, Greg Wooledge, 2017/06/09