0

I have a shell script running in my mac with contents below:

echo "Getting secrets"
aws secretsmanager get-secret-value --secret-id my-secret-id --query SecretString --output text | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' > /tmp/secrets.txt
# echo "#!/bin/sh" > /tmp/env.sh
echo "Secret retrieved, exporting"
cat /tmp/secrets.txt | sed 's/^/export my_db_/' > /tmp/env.sh
echo "echo 'Secrets exported'" >> /tmp/env.sh
rm -f /tmp/secrets.txt
echo "Done"

The script reads a db credential secret from AWS secret manager and export into a file named /tmp/env.sh. The exported file looks like below:

export my_db_username=user
export my_db_password=pwd
export my_db_engine=engine
export my_db_host=host
export my_db_port=123
export my_db_name=dev
echo 'Secrets exported'

I then run chmod -R 755 /tmp/env.sh to grant necessary permission to execute it. However, executing the /tmp/env.sh doesn't set the environment variable at all. echo $my_db_host returns me empty output. The echo message Secrets exported does get printed though.

Could anyone advise what I did wrong here?

Lee
  • 2,874
  • 3
  • 27
  • 51
  • 2
    Executing a script _never_ exports variables to the parent process that launched the script. Environment variables only go from parent to child; they _never_ go from child to parent. That's not a shell limitation, it's a UNIX-process-model limitation and applies across every programming language that exists. – Charles Duffy Jun 22 '23 at 00:18
  • (That's why you need the `eval` when you run `eval "$(ssh-agent)"` to set environment variables to values that the `ssh-agent` executable determines) – Charles Duffy Jun 22 '23 at 00:20
  • Thank you, @CharlesDuffy. This certainly helps! – Lee Jun 22 '23 at 01:25

0 Answers0