Posts Tagged ‘local’

Bash gotcha: function variable scope

Posted: 11th October 2015 by Tim in Bash, Linux
Tags: , , , , , , ,

Consider the following bash script: #!/usr/bin/env bash function myFunc {     myvar=123     echo “myFunc: setting myvar=$myvar” } myvar=1 echo “before myFunc: myvar=$myvar” myFunc echo “after myFunc: myvar=$myvar” The code here is fairly simple – we set a variable myvar, call a function and print the value of myvar to the terminal. However, even though we don’t […]