Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] Bash local variables
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 659

PostPosted: Sat Aug 22, 2020 6:03 pm    Post subject: [SOLVED] Bash local variables Reply with quote

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
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23335

PostPosted: Sat Aug 22, 2020 6:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
Tony0945
Watchman
Watchman


Joined: 25 Jul 2006
Posts: 5127
Location: Illinois, USA

PostPosted: Sat Aug 22, 2020 6:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 659

PostPosted: Sat Aug 22, 2020 6:29 pm    Post subject: Reply with quote

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
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 659

PostPosted: Sat Aug 22, 2020 6:44 pm    Post subject: Reply with quote

Tony0945 wrote:
For more information:
https://www.shellcheck.net/wiki/SC2155 -- Declare and assign separately to ...

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
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 659

PostPosted: Sat Aug 22, 2020 7:02 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum