TIL: Bash can have functions and variables with the same name
I was casually going over Bash’s man page and saw this bit about unset: if there is no variable by that name, a function with that name, if any, is unset. And it made me wonder: as unset operates on both variables and
functions, can I have a variable and a function with the same name at the same
time? That sounds absurd. But apparently, Bash disagrees.
Here’s a beautiful little snippet. We define a variable, then we define a function with the same name, and somehow both work:
$ wololo=1234
$ wololo() { echo aeoo; }
$ echo $wololo
1234
$ wololo
aeoo
WTH?
And to get rid of them, you can either:
unset wololotwiceunset -v wololo(to delete the variable) andunset -f wololo(to delete the function)
When you unset wololo, Bash first tries to delete the variable wololo.
Only if this variable doesn’t exist that Bash tries to delete the function
wololo.
That’s kind of cool, but can be super confusing.
The more I read about Bash, the more I study Bash, the more I’m puzzled by Bash.
