Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can anyone tell me why this bash script works if I cut and paste it to the terminal but throws " 7: Syntax error: "(" unexpected" when launched using $ sudo 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
	ln -s /usr/bin/gem1.8 /usr/bin/gem
	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
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


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.