0

I have a program which generates some temporary security tokens in a file. I then have to manually copy the tokens from this file and paste it in my bash profile to proceed further.Tokens expire every 30 mins, so I have to regenerate them and do the manual step of updating bash_profile every 30 mins.

I want to know weather if it is possible to programmatically update and reload .bash_profile from mac terminal when some other system file changes.

For example, I can run this command in Terminal, and it will append a line to my .bash_profile:

echo 'export Token=abc' >>~/.bash_profile

How can I update the value of Token from abc to xyz in my terminal?

NOTE: This NOT a code/script request, just looking for some direction and ideas to save time in my daily workflow.

3
  • Provide some dummy examples.
    – Biswapriyo
    Jan 4, 2019 at 20:36
  • As a long-time site user, you've probably seen the canned comment that Super User isn't a free script writing service. So I won't say that. :-) But that would be one of the reasons for the question attracting close votes. Can you tweak the question to provide more specifics, make it sound less like a script request, and maybe include as far as you've gotten with your own code?
    – fixer1234
    Jan 6, 2019 at 5:57
  • sure, let me clarify that its NOT a script request, I want to know weather if it is possible to programmatically update and reload .bash_profile when some other system file changes.
    – Himalay
    Jan 7, 2019 at 4:51

1 Answer 1

1

Doing this directly in .bash_profile (or .bashrc or any similar file) would require managing the relevant part of the file while leaving other parts intact. This problem has a simple generic solution:

  1. Do not change .bash_profile over and over again.
  2. Create like ~/.my_tokens which you would change, append to, possibly rewrite entirely.
  3. Source the new file from .bash_profile. This is the only (and one-time) change you do directly in .bash_profile:

    . ~/.my_tokens
    

It's now up to you how you manage the new file. If the file contains just few tokens, the easiest way to update may be to overwrite it entirely.

And in case of changes it may be enough to source only .my_tokens again, not the whole .bash_profile.

You can gather many such files in a directory and source them all:

# ~/my_extensions is a directory with no subdirectories
for f in ~/my_extensions/*; do
   . "$f"
done

The general idea is to separate "dynamic" tokens from your semi-static .bash_profile.


The idea of updating these tokens in every existing Bash session every 30 minutes or so seems inelegant at least. Consider reading them on-demand (not via .bash_profile at all). Let's say foo is some external command that needs these tokens. Create a wrapper function:

foo() ( . ~/my_tokens; command foo "$@" )

Now if you call foo, the function will load current tokens and call the real foo utility. Note the entire function body is in a subshell, so the tokens are not available to the current shell (unless you have separately sourced the file). To source the tokens in the current shell instead, define the function like this:

foo() { . ~/my_tokens; command foo "$@"; }

Note: for global tokens you should use some path outside any user's directory; example for Linux: /run/foo_tokens.


An atomic update of ~/my_tokens (or /run/foo_tokens or any other file) can be done with mv. You create a temporary file on the same filesystem, fill it with data, then overwrite the old version in one final step:

mv ~/temp_tokens ~/my_tokens

This guarantees any access to ~/my_tokens gets either the old version or the new one in its entirety. If you updated the file in place, it would be possible to read it as incomplete or partially updated; you certainly don't want this.

3
  • I like the approach of using a separate my_tokens file, and using foo function on demand.
    – Himalay
    Jan 8, 2019 at 15:23
  • Can you direct me to an example on how to read a file and overwrite a bash file? Meaning if I have export Token=abc in my_tokens file, I want to extract a new token xyz from another file and update my_tokens file to export Token=xyz
    – Himalay
    Jan 8, 2019 at 15:29
  • @Himalay Your comment looks like a separate question. The original one is broad enough, let's not make it broader. My suggestion is: do the research (sed?) and maybe ask a separate question; if there is no such question already, that is. Make it specific and show your research effort. How many variables are we talking about? For three variables the easiest way is to handle three separate files. In such case you already have all the pieces (writing to a new file like in the question, updating the old file atomically, sourcing many files with for). Jan 8, 2019 at 17:52

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.