if arguments is equal to this string, define a variable like this string

Alejandro picture Alejandro · Mar 15, 2012 · Viewed 429.2k times · Source

I am doing some bash script and now I got one variable call source and one array called samples, like this:

source='country'
samples=(US Canada Mexico...)

as I want to expand the number of sources (and each source has its own samples) I tried to add some arguments to do this. I tried this:

source=""
samples=("")
if [ $1="country" ]; then
   source="country"
   samples="US Canada Mexico..."
else
   echo "try again"
fi

but when I ran my script source countries.sh country it didn't work. What am I doing wrong?

Answer

Alex L picture Alex L · Mar 15, 2012

Don't forget about spaces:

source=""
samples=("")
if [ $1 = "country" ]; then
   source="country"
   samples="US Canada Mexico..."
else
  echo "try again"
fi