A lot depends on when those programs have to run and when you need their results. If it doesn't matter when they run (that is, their results don't vary with time), then it might be better to have them run from your ~/.profile
or ~/.bash_profile
(i.e., when you log in) rather than from your ~/.bashrc
.
If you need their results, but can't wait the 5 seconds it takes for them to run, what are you going to do for the first 5 seconds that your shell is up? You can't run any commands that depend on the outputs of those programs, because the results won't be available yet.
I think if you describe in more detail exactly what the requirements are, we might be able to give you better answers.
All that said, you could launch your programs in the background as Dennis Williamson suggested, and have them write their results to some files in your home directory. Then you could set the bash variable PROMPT_COMMAND
to source those files, which bash would then do before every display of PS1
.
For example, say your background program writes export ans=42
to the file ~/answer
when it's done. In your ~/.bashrc
, put
PROMPT_COMMAND=". ~/answer"
Then every time the shell displays PS1
, it will first source ~/answer
and your environment will eventually (e.g., 5 seconds after starting) contain the variable ans
with the value 42
.
Of course, the first 5 seconds after you start the shell, ~/answer
will contain the values from the last time you started a shell, so you'd have to decide whether that's important (and how to reset that file if necessary).
1ewindisch, could you add some comments to the right of the
exec
statements? I can never seem to remember how to properly (and safely!) use the<&
and>&
forms of redirection. – Zearin – 2014-01-20T16:13:53.300