Change linux password in a script, quietly Change linux password in a script, quietly shell shell

Change linux password in a script, quietly


If you want to redirect both stdout and sterr:

echo "..." | passwd &> /dev/null

which is the equivalent of

echo "..." | passwd > /dev/null 2>&1

which means "redirect stdout to /dev/null and then redirect (duplicate) stderr to stdout". This way you redirect both stdout and stderr to null ... but it might not be enough (it will be in this case I believe). But theoretically the program might write directly to terminal. For example this script

$ cat test.shecho stdoutecho stderr 1 1>&2echo stderr 2 >/dev/stderrecho stderr 3 >/dev/fd/2echo bad luck > /dev/tty$ ./test.sh &> /dev/nullbad luck

To get rid even of this output you must force the program to run in pseudo terminal, for example http://empty.sourceforge.net/ . But that is just a side note &> /dev/null will work fine.


You can also do it that way:

mkpasswd# Password:blah# BVR2Pnr3ro5B2echo "user:BVR2Pnr3ro5B2" | chpasswd -e

so the password is already encrypted in the script.


This worked for me

echo "passssssword" | passwd root --stdin > /dev/null

Notice: --stdin works for root user only