Bash - Passing arguments with "$@" to either 'sudo su' or 'sh' I have a server where shell commands to the supervisor-service must be executed by the specific user lx. I have written a bash function that does exactly that.

function supervisor {
if [ `id -nu` != lx ]; then
    sudo su - lx -c "supervisor \"\$@\"" -- supervisor "$@"
else
    sh -s "$@" &lt;&lt;EOF
supervisor "$@"
EOF
fi
}


It works, but it's doesn't look clean and I'm not really comfortable with half the things I'm using here.

How can that function be written in a less convoluted way?
The only simpler way to do this I've come up with is just to sudo every time and not worry about whether you're already the right user or not.  But overall it seems pretty straight forward and it should work fine as-is.  Some other suggestions:


Indent your code with the function.  Leaving the heredoc alone is ok.
Use [[ for conditionals.  See https://unix.stackexchange.com/a/306115/79839 for more context.
It is more modern to use $() for subcommands instead of backticks (``).  Parenthesis () are also used for creating subshells so the $() is just substituting the result of a subshell.