You read a value into the variable choice
in the main part of the script. This variable has global scope (for want of a better word), which means that it will be visible inside the function too.
Note that if you were to read the value inside the function, then the variable would still have global scope and be visible outside the function (after the function call). This would be the case unless you declared it as local
with local choice
inside the function.
For more information about scoping of shell variable, see e.g.
To pass the value in the variable choice
to the function, use
choice_func "$choice"
The function can then access this value in $1
, its first positional parameter:
choice_func () {
echo "$1"
}
or
choice_func () {
local choice="$1"
# choice is now local and separate from the variable
# with the same name in the main script.
echo "$choice"
}
This is the proper way to pass a value to a function without relying on global variables in a shell script.
local
keyword for defining local variables – steeldriver Apr 09 '20 at 14:34