Reputation: 180
i am facing an issue with this simple code , where i call a function , but the variable $X of that function doesnt seem to pass outside, since the printout message is always "Your interface is ." ... Am i missing something?
#! /bin/bash
function choose
{
echo -e " Choose your interface:"
echo -e " 1) WLan0"
echo -e " 2) WLan0mon"
echo -e " Choose: "
read -e X
if [ "$X" = "1" ]
then
X="wlan0"
elif [ "$X" = "2" ]
then
X="wlan0mon"
fi
}
(choose)
echo -e "Your interface is $X."
Upvotes: 0
Views: 89
Reputation: 271
Running (choose)
using the parentheses executes it in a subshell.
From Advanced Bash-Scripting Guide:
Variables in a subshell are not visible outside the block of code in the subshell. They are not accessible to the parent process, to the shell that launched the subshell. These are, in effect, local variables.
Removing the parentheses around choose
will make variable X
visible to your echo
.
Upvotes: 2