How do I tell a shell script my password?

I've been trying to create a script that runs sudo apt-get update and sudo apt-get autoremove whenever I log in. It goes like this:

#!/bin/bash
(sudo apt-get update)
(sudo apt-get autoremove)

But whenever I run the script it freezes my computer. I'm assuming it's because it's waiting for the script to supply a password. Would it work if I added PASSWD:mypassword after the commands like this:

#!/bin/bash
(sudo apt-get update)PASSWD:mypassword
(sudo apt-get autoremove)PASSWD:mypassword

or is there something else I should do? Is there even a way? If so please tell me!


You don't actually have to include your password in your script (from what I have seen its generally not advisable to have your password in a script). Instead you can edit your sudoer file to allow you to run the apt-get command without the need for a password.

For more information go to this website.

open a terminal (ctrl + alt + T)

Enter the command

sudo visudo

At the end of the sudoers file type

username ALL=(ALL) NOPASSWD: /usr/bin/apt-get

Where username is your username (obviously)

editing this file will allow you to run any of the apt-get commands without entering a password. This means you can also install or upgrade packages without entering your password.

You can also add other comands to this list by entering the path to the command after a comma. The path to a command can be found by entering the following command in a terminal.

which command

Where command is the name of the command( for example reboot).