help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Test exit status before printing the output without usin


From: Greg Wooledge
Subject: Re: [Help-bash] Test exit status before printing the output without using temp file
Date: Mon, 1 Feb 2016 08:23:35 -0500
User-agent: Mutt/1.4.2.3i

On Sun, Jan 31, 2016 at 05:31:08PM -0500, Dave Rutherford wrote:
> On Sun, Jan 31, 2016 at 9:49 AM, Peng Yu <address@hidden> wrote:
> > But the trailing '\n' will be removed in `$()`. Is there a way to
> > preserve the trailing '\n'?
> 
> That actually removes all the '\n''s (changing internal ones to spaces.)

You are mistaken.  Most likely you did something like this:

  x=$(command)
  echo $x

It's the SECOND line above which is wrong.  You must quote the parameter
expansion:

  x=$(command)
  echo "$x"

Then you will see that the internal newlines are preserved.

And, as already mentioned, if you want to preserve the trailing ones:

  x=$(command; printf z)
  x=${x%z}
  printf %s "$x"

The 'z' can be any character of your choice, except newline.  It's just
there to trick the command substitution into thinking the trailing
newlines are internal, so it doesn't remove them.



reply via email to

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