I think it's better to simply give an error if it's invoked as the wrong user (or even better, not provide the function at all, similar to hiding admin programs away from non-root users in
/sbin rather than
/bin). That allows a user to then consider whether they are typing into the right terminal (and any other issues), and if appropriate, issue their own
Consider this version (with the single Bashism removed, to make a portable script, and with shellcheck errors addressed):
if [ "$(id -nu)" != lx ]
echo "supervisor: can only be run by user 'lx'" >&2
command supervisor "$@"
I removed the use of the pointless inner shell - that just serves to expand arguments a second time (similar to using
$* instead of
"$@") which probably isn't what you want. Instead, I use the
command builtin to make the shell find a non-function version of
If you really must invoke
sudo from within the script, then we can simplify a bit. If users are allowed to
sudo an arbitrary command (which they should be able to, if they can
sudo su), then we can make the function invoke it like this:
sudo -u lx -- supervisor "$@"
That seems cleaner than creating a shell and funnelling arguments through it.