[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Passing variables by reference conflicts with local
From: |
Freddy Vulto |
Subject: |
Re: Passing variables by reference conflicts with local |
Date: |
Mon, 3 May 2010 23:38:52 +0200 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
I think I found a much cleaner workaround. It looks like a called
function can really unset local variables in the caller(?!), so that
passing by reference works:
_blackbox_unset_vars "$2" "$3"
If you would try to unset local variables directly in "blackbox()",
using:
unset -v "$2" "$3"
, passing by reference fails. Here's the code:
---8<-------------------------------------------------------------------
unset -v i a b d # Make sure vars are unset
# Unset variables
# Param: $* variables to unset
_blackbox_unset_vars() {
unset -v "$@"
}
# Param: $1 input argument
# Param: $2 variable name to return value to
# Param: $3 variable name to return array value to
# Public library function
blackbox() {
local a b c d e f g h i j k=( foo "bar cee" )
# ...
# Lots of library code here
# ...
# Doing a 'unset -v "$2" "$3"' directly doesn't work?
_blackbox_unset_vars "$2" "$3"
printf -v"$2" %s b # Return value "b"
printf -v"$3" x && eval $3=\(\"\${k[@]}\"\) # Return array $k
}
client() {
local i a b d
blackbox i a b; printf $'%s\n' $a "${b[@]}" # Outputs vars ok
d='ls /;true'; blackbox i "$d" "$d" # No oops
}
client
echo "globals: $i $a $b $d" # No globals created
---8<-------------------------------------------------------------------
It seems to work on bash-3.2.39, 4.0.33 and 4.1.2.
Is this behaviour safe to exploit?
Freddy Vulto
http://fvue.nl/wiki/Bash:_passing_variables_by_reference
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/01
- Re: Passing variables by reference conflicts with local, Dennis Williamson, 2010/05/01
- Re: Passing variables by reference conflicts with local, Pierre Gaston, 2010/05/01
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/01
- Re: Passing variables by reference conflicts with local, Dennis Williamson, 2010/05/01
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/01
- Re: Passing variables by reference conflicts with local, Dennis Williamson, 2010/05/01
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/02
- Re: Passing variables by reference conflicts with local, Dennis Williamson, 2010/05/02
- Re: Passing variables by reference conflicts with local,
Freddy Vulto <=
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/04
- Re: Passing variables by reference conflicts with local, Chet Ramey, 2010/05/05
- Re: Passing variables by reference conflicts with local, Chet Ramey, 2010/05/04
- Re: Passing variables by reference conflicts with local, Chet Ramey, 2010/05/02
- Re: Passing variables by reference conflicts with local, Matthew Woehlke, 2010/05/03
Re: Passing variables by reference conflicts with local, Greg Wooledge, 2010/05/03
Re: Passing variables by reference conflicts with local, Marc Herbert, 2010/05/04