Issue
I have a set of scripts that require a password to work, I coded them so that they read the password from user input instead of e.g. reading an argument or from a file so that the password is not persisted to the disk or shows on top
.
I want to start all of them and avoid having to enter the same password multiple times.
The idea is to have a parent script that reads the password once then passes it somehow to all these scripts.
So first, in parent start_all.sh
script:
read -p "pass > " pass
Now, I can't figure out how to use the value of $pass
to start up all the child scripts that need that same password.
Solution
Assuming you don't want to pass the actual password on the command line to the subordinate scripts ...
One idea using a nameref:
$ head parent child
==> parent <==
#!/usr/bin/bash
stty -echo # disable echo of password value on command line
read -p "pass > " pass
stty echo # re-enable echo of command line typing
export pass # make available to subordinate scripts
child pass # pass *name* of variable containing password
==> child <==
#!/usr/bin/bash
declare -n newpass="$1" # define nameref
echo "child/pwd: ${newpass}"
Answered By - markp-fuso Answer Checked By - Candace Johnson (WPSolving Volunteer)