Take the 2-minute tour ×

Can anyone tell me why this bash script works if I cut and paste it to the terminal but throws "server_prep.sh: 7: Syntax error: "(" unexpected" when launched using $ sudo sh server_prep.sh ?


apt-get -y install ssh libsqlite3-dev ruby-full mercurial

required_gems = ( rake rails sqlite3-ruby )

#Set up directories
[ ! -d /var/www ] && mkdir /var/www
[ ! -d /var/www/apps ] && mkdir /var/www/apps

#install gems manually
if ! which gem >/dev/null; then
	tar xvfz rubygems-1.3.5.tgz
	ruby rubygems-1.3.5/setup.rb
	gem update --system

	#Tidy Up
	rm -rf rubygems-1.3.5.tgz rubygems-1.3.5

#Install required gems
for required_gem in "${required_gems[@]}"
	if ! gem list | grep $required_gem >/dev/null; then
		gem install $required_gems

Thanks in advance!

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Your script is using sh, not bash, when it's executed. There could be a minor syntax difference between the two.

Try changing !#/bin/sh to !#/bin/bash at the top of your script

share|improve this answer

You can't have spaces when assigning variables in sh/bash. It has to be:

required_gems=( rake rails sqlite3-ruby )

In any case, it doesn't have to be an array, you could just do:

required_gems="fake rails sqlite3-ruby"

and then

for required_gem in ${required_gems}; do
  blah blah

Notice there are no "" around ${required_gems}

share|improve this answer
+1 You can usually use string lists instead of arrays. I don't think I've ever used an array in bash in the 10 years I've been programming in it. –  David Pashley Sep 13 '09 at 20:10
Indeed. The only time I use arrays in bash, is when I have several vars describing the same object. For example, when I have a service name/executable/description/sampling interval. –  katriel Sep 14 '09 at 22:05

The third to last line, should $required_gems read $required_gem?

share|improve this answer
doubleplus good. –  David Rickman Sep 14 '09 at 6:27

Your Answer


