3

I want to auto set the user password. For example I want to set the password for a no-root user to a value stored somewhere else in the script.

I am doing the following:

echo $input | passwd $user

However it fails since there is a prompt for Retype the new password.

I tried:

echo $input | passwd $user
echo $input | Retype New Password:

with no luck. It says password change aborted. How do I accomplish this? Does calling out for retyping a new password invokes another shell process?

1 Answer 1

4

Just insert a newline:

echo $input$'\n'$input | passwd $user

Or use echo -e

echo -e "$input\n$input" | passwd $user
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Ben Grimm. Worked like a charm. Can you please explain why new line works?
echoing text to STDIN of a process is effectively the same as typing, and the enter key produces a \n.
@brotherofmysister You should accept the answer if it worked for you.
sorry was offline for an hour :) Thanks @ Ben Grimm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.