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: Christoph Anton Mitterer
Subject: Re: any plans for command substitution that preserves trailing newlines?
Date: Wed, 26 Jan 2022 23:38:22 +0100
User-agent: Evolution 3.42.2-1

On Wed, 2022-01-26 at 17:08 -0500, Chet Ramey wrote:

> Bash sets locale categories internally based on LC_ALL and other
> shell variables.
Ah ok, that clears things up.


> 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.
But AFAIU, these shells would then violate POSIX in that aspect?!



> 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.

That's also clear.


> 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.

But even that's then basically not really guaranteed to work, is it?
Cause it depends on what the respective shell actually did.

Or is there a portable way to query the internal locale state of a
shell?

Consider nothing is set (neither LANG, nor LC_*)... a shell could have
set 'C' ... or 'C.UTF-8' ... or whatever.

If I now backup the old LANG/LC_* variables... most may not even be
set.
So there's no known value one can restore.


Or even if I backup the value and set/unset state of every LC_* value
before I set LC_ALL.

Once I restore afterwards, there's no guarantee that if I e.g. unset
LC_TIME again, that this unsetting will cause the internal state of
some shell to go back to whatever it was before.
Or maybe a shell anyway just acts when one of the LC_* var's is set,
not when it's unset.


Even if I try to be smart, and:
1st set all LC_* to LANG (if that was set before)
2nd set all LC_* (except LC_ALL) to their old value (or unset them if
    they were
3rd set LC_ALL to it's old value (or unset it)

it could just be that that last unsetting of LC_ALL sets e.g.
everything back to C, depending on how a given shell behaves.


And the trick of:
local LC_ALL=C
in some function, shouldn't work either, cause it would also set all
locale categories shell-wide?


So the best thing one can do is to hope, that a given shell does it
rigt, simply be unsetting LC_ALL... as bash does?!


Thanks,
Chris.



reply via email to

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