bash – Centos 8 – simple command in a shell script run with sudo privelige says “command not found”

I’m trying to run script as sudo that’s throwing errors on very simple commands on CentOS Linux release 8.4.2105 VM.

The script works fine when run without sudo. “myuser” is a valid sudo user.

Here is the script as run from the command prompt:

#sudo ./xtest.sh 

Here’s the script:

#!/bin/bash

echo "shell: $SHELL"
grep abc *
ls -l

Here’s the output when run as sudo:

shell: /bin/bash
./xtest.sh: line 4: grep: command not found
./xtest.sh: line 5: ls: command not found

Interestingly, if you “su” into root from “myuser”, the following errors display (though the login is successful):

/usr/libexec/grepconf.sh: line 5: grep: command not found
/usr/libexec/grepconf.sh: line 5: grep: command not found
/usr/libexec/grepconf.sh: line 5: grep: command not found

Typing echo $SHELL from the terminal in both myuser and root show the shell is /bin/bash

Any idea what the problem is? This script has worked on RedHat 7 and Ubuntu 8, and I believe Centos 7.

edit:

If you log into root and run the script, it works. If you run it as sudo, it has the same problem. $PATH looks like it has many repeating entries (I’ll post it above) but not sure why that would cause a problem with the running it as sudo.

Here is the result of echo $PATH from root:

usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/home/engineering/.local/bin:/home/engineering/bin:/home/engineering/.local/bin:/home/engineering/bin:/home/engineering/.local/bin:/home/engineering/bin:/home/engineering/.local/bin:/home/engineering/bin:/sbin:/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/mysql/bin

Here is the result of running with echo $PATH

 shell: /bin/bash
./xtest.sh: line 4: grep: command not found
./xtest.sh: line 5: ls: command not found
/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/bin:/usr/xpg4/

Leave a Comment