linux

How to check if user has sudo privileges inside the bash script?


I would like to check if the user has sudo privileges. This is an approximate example of what I am trying to do. I am trying to get this to work across the following os: centos, ubuntu, arch.

if userIsSudo; then
 chsh -s $(which zsh)
fi

Solution

  • sudo -l will display the commands that the user can run with sudo privileges. If there are no commands that can be run, sudo -l will return an error code and so you could try:

    sudo -l && chsh -s $(which zsh)