I need to source the file by decrypting it on the fly using the below command.

. <(gpg -qd "$encrypted_filename")

sh is not supporting process substitution. I can't use bash. Please suggest some other way.

Is there a way to source an encrypted (GPG) file on-the-fly in a script?

  • Try this command: eval gpg -d encrypted_file – Peter de Souza Jun 21 '18 at 18:35

In a second session:

mkfifo p &&
gpg -d -o p "$encrypted_filename"
# File `p' exists. Overwrite? (y/N) y

In your original session:

. p
rm p

To accomplish it in one session, and if you're comfortable with gpg Assuming "yes" on most questions, then:

mkfifo p &&
gpg --yes -d -o p file.gpg & 
. p &&
rm p

Hat tip to Outurnate's comment reminding me of gpg's --yes flag.

  • you could do this in one session by backgrounding gpg – Outurnate Nov 21 '17 at 18:55
  • I wasn't able, as I needed to provide the "y" input to gpg's "overwrite?" prompt – Jeff Schaller Nov 21 '17 at 18:56
  • 1
  • @jeff I need in one session. Basically I need to source the credentials from the encrypted configuration file. I have to add this to many scripts.so many will running parallel. Does rm file won't create issue.? – aravindderajan Nov 21 '17 at 20:18
  • You could wrap your own protection around the p fifo that's created -- by making sure it does or doesn't exist beforehand, by putting it into a mktemp directory, etc. I wanted to provide self-contained steps that cleaned up after themselves; you could keep the pipe around between invocations -- just create it once, and don't delete it. – Jeff Schaller Nov 21 '17 at 20:21
INSTRUCTIONS="$(gpg -qd $encrypted_filename)"
  • It is not working . I need to source the output of the decrypted file. – aravindderajan Nov 21 '17 at 14:20

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.