# Change password via bash script



## tmw (May 31, 2009)

Hello,
I have a question how can i change user password in bash script, this doest work (i run this script as root):


```
#!/usr/local/bin/bash
$(passwd foo) <<EOF
pass
pass
EOF
```

Is it possible to do this via bash script?


----------



## vivek (Jun 1, 2009)

Read pw command man page and look for -h FD option. Add user called foo and set password to BAR

```
echo BAR | pw add user foo -h 0
```
To modify password for existing user called tom:

```
echo jerry | pw mod user tom -h 0
```


----------



## tmw (Jun 1, 2009)

Thx for this. But i need this via passwd :/. In other words i need to type a password via script. But script must be non parametrs.


----------



## tmw (Jun 1, 2009)

Ok, i got answer. I must use expect for this .


----------



## rhyous (Jun 8, 2009)

Post an example of your script when you are done so it can be available for others.


----------



## tmw (Jun 20, 2009)

Hi, sorry for a big delay. This script will set password by passwd without user interaction:


```
#!/usr/local/bin/bash
LOG=tomek
PASS=haslo1
expect << EOF
spawn passwd $LOG
expect "New Password:"
send "${PASS}\r"
expect "Retype New Password:"
send "${PASS}\r"
expect eof;
EOF
```

This will set password "haslo1" for user "tomek".


----------



## VictorM (Dec 29, 2009)

tnx for the script - I could use this to deploy NAS boxes unattended, smart trick...


----------

