I'm trying to write a shell script so that when I reinstall my Ubuntu again. I could just run script and retain all the packages. And I'm planning to pass password in form of argument.

My question here is how can i enter into sudo domain without manually entering password. As I'm also planning to design a UI where it can't access the terminal there.


./recover.sh password

You can use...

echo password | sudo -S recover.sh

Password being your sudo password.

From sudo manpage..

-S, --stdin Write the prompt to the standard error and read the password from the standard input instead of using the terminal device.


Why not just run the script as superuser?

sudo sh recover.sh
  • ya that works when you directly access from terminal. But in UI you can only give a command .. watever you do it shoud be in the command – Pavan Apr 30 '16 at 17:07
  • Look at this askubuntu.com/questions/496204/… – Ryan Doherty Apr 30 '16 at 17:11
  • I have clearly mentioned that i want to pass password in form of argument. There is no such thing there – Pavan Apr 30 '16 at 17:22
  • That question is basically a duplicate of this and he explained why its a bad idea but he said "You can echo the password to sudo using the -s option." – Ryan Doherty Apr 30 '16 at 17:29
  • No you have misunderstood my question. Here my logic is not only limited to recovery, but it is also covering UI. And consider you are creating a UI and if u link a button with your script. Then you can only pass a command like ./install.sh password . But you cant expect the any output from terminal. – Pavan Apr 30 '16 at 17:34

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.