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: Wed, 26 Jan 2022 09:29:06 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.5.0

On 1/25/22 6:31 PM, Christoph Anton Mitterer wrote:

>> Since you can't be sure what locale variables, if any, exist in the
>> current
>> shell's execution environment, it's simpler to save and restore them
>> all.
>> If you want, you can just check whether or not LC_CTYPE (or LC_ALL,
>> depending on your preference) is set and save the original value.
>> You already know which variables you've set in the script.
> 
> ... LC_ALL might be set already, and override my own setting of
> LC_CTYPE.
> 
> Thus it's best to set LC_ALL, which overrides all.

That's the reason you should be careful -- setting LC_ALL may have other
unwanted side effects, such as behavior controlled by LC_MESSAGES or
LC_NUMERIC. You always need to understand the context.

As long as you set LC_ALL only for the scope of the assignment that strips
the trailing byte, you should be able to avoid those side effects. But to
do that you have to make sure you save and restore all the locale
variables (or use a function and local variables and let bash do it for
you), especially if LC_ALL was previously unset.

-- 
``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]