help-bash
[Top][All Lists]
Advanced

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

Re: any plans for command substitution that preserves trailing newlines?


From: Chet Ramey
Subject: Re: any plans for command substitution that preserves trailing newlines?
Date: Tue, 1 Jun 2021 09:26:06 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.2

On 5/31/21 11:45 AM, Christoph Anton Mitterer wrote:
Hey.

I wondered whether there were any plans to implement proper support for
command substitutions that preserves trailing newlines?

"Proper" is doing a lot of work in ths sentence.

If that was kinda coordinated with other POSIX sh compatible shells it
might ultimately even get standardised.

That implies it's something that people want to standardize. I haven't
seen any evidence of that.

There seem to be a number of workarounds, using bash's read (which is
however not portable, as the options needed aren't POSIX)

So there's already a solution that could be standardized if desired?

or using
sentinel values like:

foo="$( command ; rc=$?; printf 'x' ; exit $rc )"
#store rc if necessary
...

#remove the sentinel
foo=${foo%x}

but allegedly, these have numerous issues, too, depending on which
locales are used.

So this is a problem that people encounter infrequently, one that has a
solution that works 100% of the time in bash (your construct above plus
temporarily setting LANG=C before removing the last character), and doesn't
occur at all in the majority of situations (locales with UTF-8 encodings).

Why is inventing new syntax the right approach?

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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