Very old question but I have a solution that does not require the -s
flag, and does not require temperamental string operations.
A good solution might be to exploit the fact that an unauthenticated sudo
call hangs for a long time.
sudo date
will return pretty much instantly if authenticated, but will hang if not.
The timeout
command is placed before another command to limit how long it takes to execute, and exits with code 124
if it cuts the execution of the inner command short. You can check the exit code of the last command with echo $?
With that in mind...
timeout .1s sudo dateEXITCODE=$?
Now test if $EXITCODE is 127 or not and you have your answer :)