HowTo use passwords in scripts


Last modified: 31.08.2020
estimated reading time < 1 min

You might want to use passwords in scripts. as the interactive password entering is not possible in scripts, here is a work-around.

First install expect (assuming you are using Ubuntu or a similar distribution type the following:

sudo /apt-get install expect

after that the following can be used as a script – in the example below it is used to connect to a computer with ssh

#!/usr/bin/expect -f
spawn ssh
expect "assword:"
send "mypassword\r"

save the script (, close the script and make it executable by typing

chmod a+x

That’s all. Now you can execute your script by typing

Was this article helpful?
Dislike 0
Views: 28