Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Whenever I modify my .bashrc file, I have to go to each bash shell and source it again to effect the changes.

Is there a better way to do this? Some command that run once will automatically do a source ~/.bashrc in all the open bash shells?

share|improve this question
add comment

2 Answers

No I don't think this is possible. Nor should it be. This would basically amount to a way to inject code into an already launched and active shell and would pose a significant security threat.

Many daemon processes are designed to do this. The typical way would be to send them the HUP (hangup) signal which causes them to re-launch after re-reading their config files. You could trigger this with something like:

pkill -HUP daemon_name

However when doing this on bash, bash just shuts off. It's not a daemon and the system doesn't have it behave like one.

The long and the short of it is that you probably won't keep making changes to your bashrc so frequently as time goes on and it won't be much of an issue. When you do make changes, you will just have to re-source the file if you need the changes in running shells.

share|improve this answer
add comment

There's nothing built into bash. You could tell it to maybe reload .bashrc each time it displays a prompt, through PROMPT_COMMAND.

## Create a timestamp file, dated like the .bashrc that was read.
## There is a small race condition: if .bashrc is modified as the shell is
## just starting, before getting to this line, this instance won't detect
## that modification.
bashrc_timestamp_file=~/.bashrc-timestamp-$$
touch -r ~/.bashrc "$bashrc_timestamp_file"
## Remove the timestamp file on exit. The timestamp file will be left
## behind on a crash.
trap 'rm "$bashrc_timestamp_file"' EXIT HUP TERM INT QUIT
maybe_reload_bashrc () {
  if [[ ~/.bashrc -nt $bashrc_timestamp_file ]]; then
    . ~/.bashrc
  fi
}
if [[ $PROMPT_COMMAND != *maybe_reload_bashrc* ]]; then
  PROMPT_COMMAND="maybe_reload_bashrc
$PROMPT_COMMAND"
fi

This is a lot of trouble an extra file accesses for what it's worth. Also, it puts a constraint on your .bashrc: the file must be idempotent, i.e. you must be able to load it more than once with no ill effects. For example, in the snippet above, I take care to add maybe_reload_bashrc to PROMPT_COMMAND only if it's not there already.

share|improve this answer
    
@Kevin I rejected your edit because it wasn't clear what advantages / disadvantages the extra code might have. Gilles could have a side effect in mind that he was avoiding by keeping the timestamp comparison to just if the file has changed since the original and always loading on every command if it's changed even once. If you think there is really a bug, leave a comment here and I'm sure it will get fixed. –  Caleb Jun 18 '11 at 19:00
2  
@Caleb: Kevin won't be notified by your comment. For reference, the suggested edit is to run touch -r ~/.bashrc $bashrc_timestamp_file after sourcing ~/.bashrc. I actually almost wrote that, but it's not necessary: it's just been done by the sourced .bashrc. The timestamp file is updated whenever .bashrc is loaded, not just on the initial load. –  Gilles Jun 18 '11 at 21:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.