views:

95

answers:

2

Linux bash script:

function Print()
{
    echo $1
}

Print "OK"

This script runs successfully, when executed directly, and gives an error running with sudo:

alex@alex-linux:~/tmp$ ./sample-script 
OK
alex@alex-linux:~/tmp$ sudo ./sample-script 
[sudo] password for alex: 
./sample-script: 1: Syntax error: "(" unexpected

Why?

+5  A: 

Perhaps root has a different default shell that doesn't support that syntax.

Brian Rasmussen
This is likely the correct answer. If root's shell is say, dash, the `function` keyword would be invalid. That would be true for any other POSIX shell. The fact that the bash shebang worked is a strong indicator that this is exactly the problem.
Tim Post
+5  A: 

do you have

#!/bin/bash

as the first line of the script? this may be needed

Nir Levy
It's called a "shebang line" => http://en.wikipedia.org/wiki/Shebang_%28Unix%29
Milde