Script does not work when executed with sudo

Linux bash script:

  function Print ()
 {
     echo $ 1
 }

 Print "OK"

This script succeeds when executed directly and throws an error message using 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?

+4
source share
2 answers

Do you have

#!/bin/bash 

as the first line of the script? it may be required

+7
source

Perhaps root has another default shell that does not support this syntax.

+6
source

Source: https://habr.com/ru/post/1304455/


All Articles