Christoph Anton Mitterer wrote, on 25 Jan 2022:
>
> In other words, if the stdout would be as follows:
> printf 'abc\nxyz'
> would a (weird) shell be free to choose to only substitute 'abc', since
> 'xyz' is not newline-terminated... but still be conforming in the sense
> of POSIX? 
> 
> My interpretation of the above quote from the standard would be: no, a
> conforming shell needs to capture the whole stdout (except for trailing
> newlines), so the result would need to be the full 'abc\nxyz'.

You are correct, and a common method of preserving trailing newlines
is to append a non-newline character and then strip it, e.g.:

output=$(some_command && printf x)
output=${output%x}

-- 
Geoff Clare <g.cl...@opengroup.org>
The Open Group, Apex Plaza, Forbury Road, Reading, RG1 1AX, England

  • does POSIX m... Christoph Anton Mitterer via austin-group-l at The Open Group
    • Re: doe... Geoff Clare via austin-group-l at The Open Group
      • Re:... Christoph Anton Mitterer via austin-group-l at The Open Group
        • ... Geoff Clare via austin-group-l at The Open Group
          • ... Harald van Dijk via austin-group-l at The Open Group
            • ... Chet Ramey via austin-group-l at The Open Group
              • ... Harald van Dijk via austin-group-l at The Open Group
                • ... Chet Ramey via austin-group-l at The Open Group
                • ... Chet Ramey via austin-group-l at The Open Group
                • ... Christoph Anton Mitterer via austin-group-l at The Open Group
                • ... Harald van Dijk via austin-group-l at The Open Group
            • ... Geoff Clare via austin-group-l at The Open Group

Reply via email to