[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Defining variable as local -r defines variable outside the function in s
From: |
Voldemar Lazarev |
Subject: |
Defining variable as local -r defines variable outside the function in some cases |
Date: |
Mon, 13 Feb 2023 15:33:39 +0000 |
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: darwin22.1.0
Compiler: clang
Compilation CFLAGS: -DSSH_SOURCE_BASHRC
uname output: Darwin hostname_is_not_disclosed.local 22.2.0 Darwin Kernel
Version 22.2.0: Fri Nov 11 02:08:47 PST 2022;
root:xnu-8792.61.2~4/RELEASE_X86_64 x86_64
Machine Type: x86_64-apple-darwin22.1.0
Bash Version: 5.2
Patch Level: 15
Release Status: release
Description:
When a function has a variable (e.g. "X") defined as local and read-only
("local -r") and the function is called with variable of same name ("X")
specified before function name (see the "Repeat-By" section), the variable
becomes defined outside the function.
Repeat-By:
Run a Bash session.
bash-5.2$ f() { local -r a=3; echo $a; }
bash-5.2$ a=2
bash-5.2$ echo $a
2
bash-5.2$ f
3
bash-5.2$ echo $a
2
# as you can see, a is not changed
# trying again:
bash-5.2$ f
3
bash-5.2$ echo $a
2
# again, nothing is changed
# trying to specify variable this way:
bash-5.2$ a=4 f
3
bash-5.2$ echo $a
3
# external variable is changed for no reason
bash-5.2$ a=4 f
bash: a: readonly variable
bash: local: a: readonly variable
3
# and it is now defined read-only
bash-5.2$ echo $a
3
bash-5.2$ f
bash: local: a: readonly variable
3
bash-5.2$ echo $a
3
bash-5.2$
Fix:
- Defining variable as local -r defines variable outside the function in some cases,
Voldemar Lazarev <=