View previous topic :: View next topic |
Author |
Message |
halcon l33t


Joined: 15 Dec 2019 Posts: 659
|
Posted: Sat Aug 22, 2020 6:03 pm Post subject: [SOLVED] Bash local variables |
|
|
From https://linux.die.net/abs-guide/localvar.html :
A variable declared as local is one that is visible only within the block of code in which it appears. It has local "scope". In a function, a local variable has meaning only within that function block.
Could anyone enlighten me why that rule does not apply in my (simplified for testing) script?
testing.sh
Code: | #!/bin/bash
function one {
echo "one: CATEGORY_NAME=$CATEGORY_NAME"
}
function two {
echo "two: CATEGORY_NAME=$CATEGORY_NAME"
}
function three {
for MY_CATEGORY in $(find /home -maxdepth 1 -mindepth 1 -type d | sort); do
local CATEGORY_NAME=$(basename "$MY_CATEGORY")
one
done
}
three
two
exit 0 |
The otput
Code: | # testing.sh
one: CATEGORY_NAME=halcon
one: CATEGORY_NAME=lost+found
two: CATEGORY_NAME=
# |
I don't pass CATEGORY_NAME as argument to one, nonetheless one sees its value.
Yes, one is called in the same block (function) where CATEGORY_NAME is declared, but it's definitely not the same that is written ^^^^^ in bold. In a function, a local variable has meaning only within that function block!
Last edited by halcon on Sat Aug 22, 2020 7:03 pm; edited 1 time in total |
|
Back to top |
|
 |
Hu Administrator

Joined: 06 Mar 2007 Posts: 23335
|
Posted: Sat Aug 22, 2020 6:21 pm Post subject: |
|
|
You should rely on better documentation, such as info bash, which states (in part; full paragraph truncated): Code: | 'local'
local [OPTION] NAME[=VALUE] ...
For each argument, a local variable named NAME is created, and
assigned VALUE. The OPTION can be any of the options accepted by
'declare'. 'local' can only be used within a function; it makes
the variable NAME have a visible scope restricted to that function
and its children. | Thus, it is correct that the variable remains visible in functions called from the scope where it is visible. It is not visible after you finish the scope in which it was declared local. |
|
Back to top |
|
 |
Tony0945 Watchman

Joined: 25 Jul 2006 Posts: 5127 Location: Illinois, USA
|
Posted: Sat Aug 22, 2020 6:24 pm Post subject: |
|
|
Code: | ~ $ shellcheck junk
In junk line 18:
local CATEGORY_NAME=$(basename "$MY_CATEGORY")
^-----------^ SC2155: Declare and assign separately to avoid masking return values.
For more information:
https://www.shellcheck.net/wiki/SC2155 -- Declare and assign separately to ... |
I routinely use "junk" for throway files. No disrespect intended. |
|
Back to top |
|
 |
halcon l33t


Joined: 15 Dec 2019 Posts: 659
|
Posted: Sat Aug 22, 2020 6:29 pm Post subject: |
|
|
Hu,
Thank you for your reply!
So, function and its children. I got it.
...Used to use to man and --help, but not info. Gone to search how to use info...
EDIT:
Thanks to you, Tony0945!
What disrespect, it's OK... Already reading the page that you linked. |
|
Back to top |
|
 |
halcon l33t


Joined: 15 Dec 2019 Posts: 659
|
Posted: Sat Aug 22, 2020 6:44 pm Post subject: |
|
|
Looks like I knew that rule about declaring and exporting separately before, but have successfully forgotten it by now, because I looked at my .bashrc recently and wondered: why I declare and export in different lines? I was told to write comments about (nearly) all the changes to config files... I'm going to do that from now on, for sure  |
|
Back to top |
|
 |
halcon l33t


Joined: 15 Dec 2019 Posts: 659
|
Posted: Sat Aug 22, 2020 7:02 pm Post subject: |
|
|
Hu,
I've found a code example in info bash, almost the same as my code:
Code: | func1()
{
local var='func1 local'
func2
}
func2()
{
echo "In func2, var = $var"
}
var=global
func1 |
|
|
Back to top |
|
 |
|