0

I've split up my .profile and .bashrc files like so based on various advice I've read. I have an alias bin which I use to refresh my environment after changing .profile and .bashrc to avoid exiting and re-opening the shell. However, there are two problems currently:

  1. My $PATH will grow for each refresh because I prepend the default $PATH.
  2. I only want fortune when I start a new shell, not when refreshing it with bin.

What is the most correct, non-hackish way to refresh my environment without the unwanted side-effects? (Copy-pasting the system path into my .profile is not an option!)

.profile

# LOTS OF EXPORTS
export ***
export ***
export PATH=.:$HOME/bin:***:$PATH

shopt -s extglob
[ -n "$BASH_VERSION" ] && [ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"
fortune

.bashrc

# LOTS OF ALIASES
alias ***
alias ***
alias bin='source $HOME/.profile; set +h'

# LOTS OF FUNCTIONS
foo() {}
bar() {}
1

The easiest and most non-hackish way to refresh the environment is to quit the current shell session and start a new one.


You may also start a new login shell from the current shell, but that may have unwanted consequences, such as modifying PATH even though you've already modified your PATH (environment variables are inherited by the new shell, and not reset to what they would have been if you had started a new shell in a new terminal).

You could also start a new shell with env -i bash -l, where env -i would clear the environment before invoking a new bash login shell. This may not be the same as starting a new shell in a new terminal either, as the inherited environment would not be the same.

  • The first suggestion says /bin/fortune: No such file even though fortune is in /usr/local/bin, which is in my PATH. Also no aliases are set. The second gives me an almost empty env (only PWD & SHLVL are set), but all my aliases seem to be set. – forthrin May 12 '18 at 13:17
0

Consider using an inline test and export your path conditionally in .bashrc

[[ $PATH =~ "$HOME/bin" ]] && export PATH="${HOME}/bin:${PATH}"

As for the fortune command, you can have it run on login by adding it in .bash_profile which is executed by only login shells. You can force your Terminal emulator to start a Login shell in the app's preferences.

  • The .profile file is used if .bash_profile does not exist, so your comment about this file is not needed. Also, inserting guards in the files is a bit hackish... – Kusalananda May 12 '18 at 8:30

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.