help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: All bash variables that can be used as temp variables?


From: Peng Yu
Subject: Re: All bash variables that can be used as temp variables?
Date: Wed, 25 Jan 2023 11:12:28 -0600

On 1/25/23, Koichi Murase <myoga.murase@gmail.com> wrote:
> 2023年1月22日(日) 6:47 Peng Yu <pengyu.ut@gmail.com>:
>> https://lists.gnu.org/archive/html/help-bash/2023-01/msg00104.html
>>
>> As discussed here BASH_REMATCH is a temp variable that can be used to
>> avoid polluting the global variable space.
>
> BASH_REMATCH can be used to extract a part of the string using a
> regular expression, but that doesn't mean it doesn't affect the global
> variable space. It's actually the opposite. Currently, using regular
> expressions in [[ ]] would always cause side effects in **BASH_REMATCH
> of the global scope**. One needs to always copy the content of
> BASH_REMATCH to other variables when one wants to use it after calling
> some other functions.
>
>> What other bash variables
>> can be used as temp variables (not necessarily for the purpose of
>> trimming prefixes and suffixes) to avoid polluting the global variable
>> space?
>
> If you mean that using BASH_REMATCH doesn't cause additional pollution
> of the global space because it's already polluting the global space,

Yes. That is what I meant. Sometimes, I will need multiple temp
variables. Just having one temp variable BASH_REMATCH may not be
sufficient.

> you could check the list of the already used global variable names in
> Bash Reference Manual Sec. 5.2:
>
> https://www.gnu.org/software/bash/manual/bash.html#Bash-Variables

I know this list. But what are the safe ones to use? I know REPLY can
be used when `read` is not used. I don't know what else can be safely
used. For the ones that I am familiar with, I know that they are
better not to be used as temp variables. For the other variables that
I am not familiar with, I don't have enough knowledge to know whether
and when they can be safely used as temp variables.

-- 
Regards,
Peng



reply via email to

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