Skip to content

simple bash function to prefix any given command with sudo if needed

The title of this entry tells it all. I've create a simple bash function to prefix any given command expression with sudo if needed. If needed means, if you are not root. It is totally simple but to be true, it took me time to use my bash skills to write it. Imagine you now how to draw a cloud, the sun, a tree and a house and once you figure out how to concatenate all of that elements to draw a picture for your parents ;-).

if [[ $# -lt 1 ]];
then
    echo "Invalid number of arguments provided"
    echo "${FUNCNAME[0]} <command to execute>"
    return 1
fi

if [[ $(whoami) == "root" ]];
then
    [email protected]
else
    sudo [email protected]
fi

Looks like Chuck Norris is using github right now. Github is down, otherwise I would add a link to the fitting repository commit. Github is back online, here is the promised link to the commit.

Übersetze nach en fr

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

The author does not allow comments to this entry

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Markdown format allowed
Form options