1

How would I be able to catch the input for a substituted command stored in a variable?

Example:

How would I be able to catch the input for the following command?

bar_foo=$(openssl passwd -1 -salt "foobar")
2

The approach you're showing above would catch the output from the subcommand, ie. $(...) into your variable, $bar_foo. When using openssl you can provide the password using either the -in or the -stdin switches.

Example

$ myvar=$(openssl passwd -1 -salt "foobar" -stdin <<< "blah")
$ echo $myvar
$1$foobar$1ips4/cyJvjUjCj8w4exx0

It's generally advisable to put the password in a file and then call openssl, since that'll keep it out of your history and shield it from being exposed too openly.

UPDATE #1

The OP ended up using this method to prompt the user for their password using read and then storing it in a variable, pass1.

$ read -p "Password: " -s pass1

This variable could then be used as input to the openssl command.

$ myvar=$(openssl passwd -1 -salt "foobar" -stdin <<< "$pass1")
$ echo $myvar
$1$foobar$1ips4/cyJvjUjCj8w4exx0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.