This is related to question Why is this bypassing the SUDO password?. In that one of the responders answered the question in an awesome way, and very, very clear to any newbie. Now here is a question that I can't seem to figure.

I wrote a script for starting the vmware firefox plugin (don't worry. I gave that up and now run vBox very happily. I left vmware for my servers :) )

I needed to start the plugin as sudo, but I also needed to pass an argument (password) to it, that happened to be the same.

So, if my password was Hello123, the command would be: sudo ./myscript.sh hi other Hello123

Running from the command line, the script would ask for my sudo password and then run. I wanted to capture that password and pass it as well. I also wanted to run graphically, so I tried gksudo, and there is an option -p that returns the password for variable assignment.

Well, that was a nightmare because I would still get prompted for the original sudo: see below

Find UserName

Find password (and hopefully enable sudo)

vP=gksudo -p -D somedescriptiontext echo
Execute command

gksudo ./myscript.sh hi $vUser $vP

And I still get prompted twice.

So my question is tri-fold:

  1. Is there a variable I can use for the password, just like there is one for user, $USER?

  2. Is there a different way I should be assigning the value resulting of the command I have in $vP? I am wondering if executing the way I have it, does it in an uninitiated session and not the current one, since I am getting some additional warning type errors on some variables.

  3. I tried using Zenity to just capture the text, but then of course, I couldn't pass that value to sudo, so I could only use it as a parameter, which puts me back in 2 prompts.

3 Answers 3


You can assign certain commands to run without having to provide a password with sudo (man visudo). For example, you might have a line like this, if your username is psusi:

%psusi ALL = NOPASSWD: /usr/local/bin/myscript.sh

Use extreme caution with granting all-trusting sudo access.

  • That sudoers line will give all members of the group psusi allowance to run that script without typing the password...
    – geirha
    Feb 2, 2011 at 8:40

The program you're looking for is called expect. You write a script wherein you tell the program to expect certain prompts (such as password prompts) and then supply some response via stdin, the same as with keyboard input. I've used it in the past to get around annoying password prompts that I couldn't (easily) get rid of.

sudo aptitude install expect
man expect

1) No, since that would give away your password to anyone on your system

2) The return value from gksudo is an integer indicating whether it had an error or not. The -p switch has it PRINT the password to its stdout.

3) Why are you even trying to get the password? Skip the first call to gksudo and just use the second.

  • Thanks for answering. The reason i need the pwd is because i need it as a param for what i am passing. So let me give you the actual scnario. vmware server in ubuntu, comes with a "viewer" for firefox, that you can start separately (given that vmware doesn't have a client). When you open it, you need to put in your sudo user and pwd, and then you pick the machine to connect. however it also allows you to pass this info from command line, to skip the prompt. So i need to launch it with sudo AND need to pass the pwd as a parameter. That's why i capture it via stdout. makes sense? Jan 12, 2011 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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