Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've been working on a script that automates setting up a development environment for Raspberry Pi development (step by step details that work are here). The script is linked in that article but convenience you can find it here also. Now when run this script install and sets up the environment without error but you have to enter your sudo password more than once due to sudo's time-out value by default. So I started experimenting by removing all the sudo lines and running the whole script via sudo at the command line like so:

kemra102@ubuntuvm:~$ sudo ./pi_dev_env_install.sh

This works fine as expected and gets most of the way through until this point:

./pi_dev_env_install: 68: ./pi_dev_env_install.sh: Syntax error: "(" unexpected

Now this line worked fine previously when not running the whole script with sudo. There is nothing about this line running as sudo that should stop it working to my knowledge, does anyone have any ideas?

share|improve this question
    
The shebang is really in line 9? Due to Ubuntu's DashAsBinSh affinity I suspect your script is interpreted by dash instead of bash. Try to move the shebang in line 1. –  manatwork Aug 18 '12 at 11:45
    
According to that article calling /bin/bash directly instead of /bin/sh will; correctly use bash instead of dash so that should not be an issue as I understand it. I can still move the shebang of course, but that doesn't really explain as to why it works when you don’t sudo the whole script. –  kemra102 Aug 18 '12 at 11:50

3 Answers 3

up vote 17 down vote accepted

The script does not begin with a shebang line, so the kernel executes it with /bin/sh. On Ubuntu, /bin/sh is dash, a shell designed for fast startup and execution with only standard features. When dash reaches line 68, it sees a syntax error: that parenthesis doesn't mean anything to it in context.

Since dash (like all other shells) is an interpreter, it won't complain until the execution reaches the problematic line. So even if the script successfully started at some point in your testing, it would have aborted once line 68 was reached.

The shebang line must be the very first thing in the file. Since you use bash features, the first line of the file must be #!/bin/bash or #!/usr/bin/env bash.

share|improve this answer
    
Thanks clearly a gap in my knowledge, I don't script much so wasn't aware of that! Thanks for the explanation it's helped a lot and will be very useful to know in the future too. –  kemra102 Aug 18 '12 at 12:25
    
Let me add that this error occurs even while using scripts extension for nautilus. Adding the shebang line solved it immediately. +1. –  bkd.online Oct 9 '13 at 5:14

If the shebang is not on the first line, it will not be respected, regardless of the shell of the root user, the SHELL variable or the -s flag. You can easily confirm this is with a simple example:

#
#!/bin/bash
offfset=(`ls`)
echo $offset

Running this script with sudo will raise a syntax error in recent versions of Ubuntu and Debian.

You have two options to make sure the script is interpreted by bash:

  1. Move the shebang to the first line

  2. Run sudo like this:

    sudo bash ./pi_dev_env_install.sh
    
share|improve this answer
    
Thanks for the test, very useful. –  kemra102 Aug 18 '12 at 12:26

Maybe you have a "(" on the dir or file name.

share|improve this answer
1  
The missing shebang was already accepted as the answer by the original poster. –  Philipp Claßen Jan 22 '13 at 22:58

Your Answer

 
discard

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.