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")
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.
$ 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.
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