Alex F
Alex F

Reputation: 43331

Script doesn't work when executed with sudo

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?

Upvotes: 4

Views: 983

Answers (2)

Nir Levy
Nir Levy

Reputation: 4740

do you have

#!/bin/bash

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

Upvotes: 7

Brian Rasmussen
Brian Rasmussen

Reputation: 116501

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

Upvotes: 6

Related Questions