I would like to know if there is a safe way to only be queried for a password once in a custom bash script, and then save it somehow to be used for later needs. At the end of the script it should be shredded in some way so it won't be recoverable from memory or in any other way.
gpg-agent and provide your password as:
__password=$(gpg --decrypt /path/to/password.gpg)
in your script.
Of course, you need to previously encrypt it:
$ echo "correct_horse_battery_staple" > password $ gpg --encrypt password