How to check if sudo password has been entered for this terminal session?
You can use:
if sudo -n true 2>/dev/null; then
echo "I got sudo"
else
echo "I don't have sudo"
fi
The -n
(non-interactive) option prevents sudo
from prompting the user for a password. If a password is required for the command to run, sudo
will display an error message (redirected to /dev/null
) and exit. If the password is not required, then this expression is true: sudo -n true 2>/dev/null
.