help-bash
[Top][All Lists]
Advanced

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

Issues modifying a local variable inside of a subfunction


From: Hunter Wittenborn
Subject: Issues modifying a local variable inside of a subfunction
Date: Mon, 11 Oct 2021 21:08:46 -0500
User-agent: Zoho Mail

I'm currently having some issues modifying shell variable that have been 
declared with 'local' via subfunctions in a given function.



The following gives what my actual script is trying to do:



"""

#!/usr/bin/env bash

test1() {

  variable="${1}"

  declare -g "${variable}=1"

}



test2() {

  local number=0

  test1 number

  echo "${number}"

}



test2

"""



Why is this outputting "0" instead of "1"? After reading over the Bash man 
pages I was thinking 'declare -g' might be assigning to a variable reference 
outside of that made by my 'local' command, but at that point I can't figure 
out how I can modify the value of 'number' while inside a subfunction of the 
'test2()' function.



Is this a bug, or is it just behaving as expected from how I described above? 
If the latter, is there any way to accomplish what I'm wanting to do?



---

Hunter Wittenborn

mailto:hunter@hunterwittenborn.com

https://www.hunterwittenborn.com

https://github.com/hwittenborn


reply via email to

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