|
From: | Chet Ramey |
Subject: | Re: any plans for command substitution that preserves trailing newlines? |
Date: | Wed, 2 Jun 2021 11:23:52 -0400 |
User-agent: | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.2 |
On 6/2/21 11:15 AM, Greg Wooledge wrote:
That's the basic idea, yes. I would save the locale's variable values, use LC_CTYPE instead of LC_ALL, put the assignment to LC_CTYPE as its own command (though that's not strictly necessary), and make sure to restore the locale afterward. 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.Are we talking about bash or sh here? If it's bash, you can use a function with LC_ALL or LC_CTYPE scoped as a local variable.
It's bash, and I'm recommending the simplest solution without getting into shell functions. With any luck, you should be able to use the same approach with other shells and not have to depend on their treatment of local variables, if the OP wants to explore more portability questions. Saving and restoring the variables is the most straightforward if you're interested in portable use. -- ``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/
[Prev in Thread] | Current Thread | [Next in Thread] |