I have a bash function defined in a global bashrc, which requires root privileges to work. How can I run it with sudo, e.g. sudo myfunction. By default it gives an error:

sudo: myfunction: command not found

link|improve this question

2  
Never tried, but this blog post seems to handle it: w00tbl0g.blogspot.com/2007/05/… – Grizly Sep 3 '10 at 12:16
feedback

5 Answers

Maybe you can do:

function meh() {
    sudo -v
    sudo cat /etc/shadow
}

This should work and saves you from typing sudo on the commandline.

link|improve this answer
Depending on your system... this will prompt you for every call of the sudo command to enter the password... or prompt you once & cache it. It would be better to detect if you're running as root, and if not... call the bash script again with sudo once. – TheCompWiz Sep 3 '10 at 14:00
I have yet to encounter a system that does not cache the sudo password: the default for timestamp_timeout is 5. If you set it to 0, you are always asked for a password, but that would be a custom setting. – wzzrd Sep 3 '10 at 14:36
feedback
#!/bin/bash

function smth() {
    echo "{{"
    whoami
    echo "}}"
}

if [ $(whoami) != "root" ]; then
    whoami
    echo "i'm not root"
    sudo $0
else
    smth
fi
link|improve this answer
feedback

You can export your function to make it available to a bash -c subshell or scripts that you want to use it in.

your_function () { echo 'Hello, World'; }
export -f your_function
bash -c 'your_function'

Edit

This works for direct subshells, but apparently sudo doesn't forward functions (only variables). Even using various combinations of setenv, env_keep and negating env_reset don't seem to help.

Edit 2

However, it appears that su does support exported functions.

your_function () { echo 'Hello, World'; }
export -f your_function
su -c 'your_function'
link|improve this answer
+1 , I would say this is the correct answer. – Kyle Brandt Sep 6 '10 at 17:52
whether this method works ?? In my case it is not. – pradeepchhetri Mar 26 at 12:04
@pradeepchhetri You may want to give more information, such as what you are precisely trying, which shell you use, and which OS you use. – Legolas Mar 26 at 13:23
@Legolas: I am trying the same thing script wrote in the above script. I am getting the error bash: your_function: command not found. I am using Ubuntu 11.04 and bash shell. – pradeepchhetri Mar 26 at 13:57
@pradeepchhetri what if you use sudo -E bash -c 'your_function'? – Legolas Mar 26 at 14:33
show 5 more comments
feedback

I would execute a new shell by having sudo execute the shell itself, then the function will run with root privileges. For example something like:

vim myFunction
#The following three lines go in myFunction file
function mywho {
    sudo whoami
}

sudo bash -c '. /home/kbrandt/myFunction; mywho'
root

You could even then go to make an alias for the sudo bash line as well.

link|improve this answer
feedback
sudo bash

then run your command

link|improve this answer
So a comment to go with the downvote would be appreciated... – gWaldo Sep 3 '10 at 14:57
I didn't downvote this answer but I suspect its because the question was asking how to get the command to run in his global bashrc or in other words he wants to know how to run the command as root without being prompted so the bash doesn't fall apart when trying to parse the line with the function he is trying to execute. – Wilshire Jun 18 '11 at 16:51
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.