Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

exactly i want to make a script and add this to shortcut launcher...
when i double click the icon, it will popup a terminal window (done)

in terminal, it will ask root password at first..
and then do some stuff with root privilege (i make it as a function) ..
after that, in the end of script it will close the window
like "press anykey to close window" (i can do it)

sudo -S touchpad # but it return error "command not found"

how to run a function as root user?
or there is another way to do this?

i'v try this #!/bin/bash

sudo ./ #this not work when i make it as a launcher

this is my current code (after googling):


gksudo -m "Input Password" clear
sudo whoami
#sudo bla bla bla
read -n 1 -p "Press any key to close window"

this is what i want, but got error if i type wrong password for 3 times..
how to force quit main window if gksudo fail??

share|improve this question
up vote 2 down vote accepted

Test the return value of gksudo:

if ! gksudo -m "Input Password" clear ; then
    echo Bad password
    exit 1
share|improve this answer

You can't. To do it, you need to put the function in another script, and run that script with sudo.

Sudo only runs commands, it cannot access functions inside your script, as it runs in a separate process. If you insist on running a function in your script, you could run the script again with sudo, but pass it an argument with $1 that runs the function directly.

share|improve this answer
i have try to put my function in another script, it works when i acces it via terminal. but when make it as a launcher(application interminal) in panel it didn't works... see my edit.. – Warung Nasi 49 May 11 '11 at 7:16
Please try it using the full pathname: sudo FULL_PATH_TO/ . When called from the launcher the initial working directory is not the script directory, using ./ is not expected to work. – João Pinto May 11 '11 at 11:42

what about using gksudo instead? It will prompt user for entering password. Then it should work with just sudo.

share|improve this answer

IMHO ... Simplest way to run the entire script as root without having to remember that it needs to be run as root It seems like it should work for what you are trying to accomplish

if [[ ${UID} -gt 0 ]] ; then
    sudo ${dScript}
### the rest of your script
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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