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: Koichi Murase
Subject: Re: any plans for command substitution that preserves trailing newlines?
Date: Wed, 2 Jun 2021 09:15:14 +0900

2021年6月2日(水) 6:05 Christoph Anton Mitterer <calestyo@scientia.net>:
> So basically:
> -------------------------------------
> LC_ALL=<whatever>
>
> subst="$( command; rc=$?; printf 'x'; exit $rc )"
>
> #remember rc if necessary
> rc=$?
>
> #remove the sentinel
> LC_ALL=C subst="${subst%x}"
> -------------------------------------
> is guaranteed to work for every locale and every sentinel value (not
> just 'x')... and should also be portable, right?

It should be mostly portable, but the Stack Exchange page says that it
fails in yash.

> And:
> For UTF-8, the same work even when not setting LC_ALL=C at the removal,
> right?

It seems most implementations work according to the Stack Exchange
page, but we cannot ensure. It depends on how misencoded data is
handled by the shell implementation. Why you don't want to set
LC_ALL=C.

> What I coudln't find out was:
> Does:
>   LC_ALL=C subst="${subst%x}"
> work as I'd expect, that is LC_ALL being active *ony* for the
> assignment and parameter expansion?

No. You haven't provided the command so the effect of the variable
assignments persists after this line.

> I've read in POSIX' "2.9.1 Simple Commands":
>
> [...]
>
> But when I tried it out, the environment wasn't affected, i.e.
> 1) Before:
> [...]
>
> 2) Removal:
> $ LC_ALL=C c=${b%$s}
>
> 3) After:
> $ locale

You forget to export LC_ALL. locale doesn't know the value of LC_ALL
in the parent Bash process, but it just sees the exported values of
LC_* and LANG.

I think you should read the original Stack Exchange page more
carefully. Most of the answers are written there.

--
Koichi



reply via email to

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