the installation of the above script require 'set alias sudo=sudowrap' which is not recommendable imho. Please see my answer for a solution which doesn't require anything to work.
–
Luca BorrioneSep 2 '12 at 10:24

The one disadvantage to this approach is that it only expands the function you're calling, not any extra functions you're referencing from there. Kyle's approach probably handles that better if you're referencing functions that are loaded in your bashrc (provided it gets executed on the bash -c call).

On ServerFault, it's preferred you show the code that's desired as well as linking to the external site, so users don't have to click away to get the information they want, and so the information survives the potential death of external sites.
–
Conspicuous CompilerJan 15 at 22:23

+1 , I would say this is the correct answer.
–
Kyle Brandt♦Sep 6 '10 at 17:52

1

whether this method works ?? In my case it is not.
–
pradeepchhetriMar 26 '12 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.
–
LegolasMar 26 '12 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.
–
pradeepchhetriMar 26 '12 at 13:57

@pradeepchhetri what if you use sudo -E bash -c 'your_function'?
–
LegolasMar 26 '12 at 14:33

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.
–
TheCompWizSep 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.
–
wzzrdSep 3 '10 at 14:36

calling without sudo
Hello yourname!
You passed the following params:
first
second

calling with sudo
Hello root!
You passed the following params:
-n
john done
-s
foo

If you need to use this in a shell calling a function which is defined in your bashrc, as you asked, then you have to put the previous exesudo function on the same bashrc file as well, like the following: