Setting a variable inside a case statement

Viewed 27

I am pretty new to BASH, had a good look around and cannot seem to get my head around the syntax for a case statement.

I basically want to allow the user to select a region and then let the case statement assign that to a variable called $region, which will be used later in the script.

Any assistance most appreciated.

echo 'u = UKI'
echo 'e = EU'
echo 'a = NA'

  read choices;
  case "$choices" in

u) region = "eu-west-2" ;;
e) region = "eu-central-2" ;;
a) region = "us-east-3" ;;
*) Invalid Choice ;;
esac

echo $region

I seem to be getting a : region: command not found error.

1 Answers

You just need to remove spaces, as in bash you should use = without spaces to assign a var, like region="eu-west-2", not region = "eu-west-2"

As a result:

echo 'u = UKI'
echo 'e = EU'
echo 'a = NA'

  read choices;
  case "$choices" in

u) region="eu-west-2" ;;
e) region="eu-central-2" ;;
a) region="us-east-3" ;;
*) Invalid Choice ;;
esac

echo $region
Related