BASH Function to Check if sudo Available


sudo is the command to allow normal users perform some escalated operations. We can use the following BASH function to check to see if the sudo command is available. This is done via the which command which searches the command in the $PATH path.

1
2
3
4
5
6
7
8
9
# 0 - ok
# 1 - nok
is_sudo_available()
{
    local result=1
    which sudo 2>&1 1>/dev/null
    [ $? -eq 0 ] && result=0
    return $result
}
# 0 - ok
# 1 - nok
is_sudo_available()
{
    local result=1
    which sudo 2>&1 1>/dev/null
    [ $? -eq 0 ] && result=0
    return $result
}

This function works in BASH script on Linux platforms and also the Windows Subsystem for Linux or Cygwin.

BASH Programming/Shell

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
169 words
Last Post: Teaching Kids Programming - Find the Difference of Two Almost Same Strings
Next Post: Teaching Kids Programming - Concatenation of Arrays

The Permanent URL is: BASH Function to Check if sudo Available

Leave a Reply