-1

Currently, I have this bash script:

#!/bin/bash
function clear_secrets {
  export bob=""
  export john=""
}
clear_secrets

And I want this bash script to run every 1 minute through launchd. However, when I set those environment variables in a bash session, they are not being cleared out after a minute. I'm assuming it's because launchd runs the script in a different session. Is there a way to have launchd affect all bash sessions?

To clarify, I want to clear some environment variables every minute in all current bash sessions. I tried this using a bash script and having launchd sourcing that bash script every minute. What can I do to achieve this?

migrated from stackoverflow.com Sep 6 '13 at 7:08

This question came from our site for professional and enthusiast programmers.

  • 3
    No. Please describe the actual problem you're trying to solve instead of what you perceive as the solution. – Ansgar Wiechers Aug 27 '13 at 22:54
  • I don't think you can clear a variable on a launched application since it was already exported and the context of it is already in the launched application. Unsetting the export on your current shell that previously launched the application who inherited the variables won't unset those. – konsolebox Aug 27 '13 at 23:03
  • 1
    Also, if you're planning to unset the export for your current session, just use . yourscript.sh, not bash yourscript.sh or ./yourscript.sh since it would launch another bash process where the exported variables could be unset and not of the calling shell. – konsolebox Aug 27 '13 at 23:05
  • 2
    Each process has its own separate set of environment variables. The export command only exports a variable to subprocesses created by that particular shell process (and only those it spawns after the export command). There is no way for one shell to change the environment of another running process. – Gordon Davisson Aug 28 '13 at 6:26
0

Try changing your script to:

#!/bin/bash
function clear_secrets {
  export bob=""
  export john=""
}
while true
do
  clear_secrets
  sleep 60
done

And then update /etc/bashrc (or where ever the default bashrc is for your system) to call this script. something like:

/usr/local/bin/clearSecrets &

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.