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 17:08:04 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.5.1

On 1/26/22 4:33 PM, Christoph Anton Mitterer wrote:
> On Wed, 2022-01-26 at 14:53 -0500, Chet Ramey wrote:
>> I suppose you can get away without doing it, as long as you pay
>> attention
>> to which of those variables are exported (and so affect other
>> processes'
>> locale state) and which are not and affect only the shell, and make
>> sure
>> that the internal and external state is consistent before and after
>> you
>> do your thing. Bash will reset the state properly for you, but you
>> don't
>> know what other implementations will do.
> 
> Sorry but I still don't get it.
> 
> From how you write it sounds as if setting LC_ALL to some value would
> also actually set the values of other LC_* ?

Yes, setting LC_ALL sets all locale categories, even if there is a variable
specific to that category. They're not necessarily reflected in any shell
variables because of the locale variable precedence hierarchy. I'm not
talking about variables, though, I'm talking about the shell's locale
state.

> 
> But that doesn't seem to be mandated by POSIX:
> "LC_ALL
>     This variable shall determine the values for all locale categories.
> The value of the LC_ALL environment variable has precedence over any of
> the other environment variables starting with LC_ (LC_COLLATE,
> LC_CTYPE, LC_MESSAGES, LC_MONETARY, LC_NUMERIC, LC_TIME) and the LANG
> environment variable."
> 
> Neither does bash seem to set/reset anything (regardless of exported or
> not):

Bash sets locale categories internally based on LC_ALL and other
shell variables. It has to do this itself via explicit calls to setlocale()
because it's expected to modify the shell's locale settings based on the
values of shell variables. Other shells may not do that, and even programs
that shell starts may not use the same locale settings if the shell
variables are not exported.

For instance, let's say the shell starts with an inherited environment
variable LANG=fr_FR.UTF-8, and no LC_ variables. You can set the shell
variable LC_ALL=C and the shell's locale settings will reflect that (or
LC_CTYPE, or whatever, to set individual categories). Not all shells will
do that, but bash will. Programs that the shell starts will get different
locale settings based on LANG, since it's in the environment (even programs
started by command substitutions) unless you've exported LC_ALL.

So if you want to temporarily control the locale a command substitution, or
any program the shell runs, gets, you have to save, set, export, and then
optionally reset all the variables you care about. I'm not saying this
doesn't give you a lot of freedom, but you do have to think about how
environment variables and child processes affect it.

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