Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  29] [ 0]  / answers: 1 / hits: 6414  / 1 Year ago, sat, april 22, 2023, 5:13:09

I'm having trouble wrapping my head around what belongs in ~/.profile and what belongs in ~/.bashrc.


From what I've read, it seems to me that ~/.profile should be used for environment variables and ~/.bashrc for aliases, functions, and the like. If I move all my export statements from ~/.bashrc to ~/.profile, would everything work as it should, or am I about to break something?


(Here's my dotfiles repo if you want to look over both files.)


EDIT 2022-06-03: I've moved most of my environment variables from ~/.bashrc to ~/.profile and have assured myself that ~/.bash_profile sources ~/.profile. The only environment variables I've left in ~/.bashrc are those that only matter when I'm working in a shell, e.g., those pertaining to less, the prompts, slrn, and the like. Everything seems to be working well. Thanks for the help.


More From » bashrc

 Answers
4

It helps to understand which files get sourced when and why.


.profile is sourced by a login shell on startup. Typically, the only login shell you start is the one started when you log in, but you can run a login shell at any time with bash -l. (Also, on macOS, there is no initial login shell, so terminal emulators tend to run a login shell for each new window.)


.profile is an ideal place to set environment variables that can be inherited by any program started from the login shell.


.bashrc, on the other hand, is sourced by non-login interactive shells, such as those started by terminal windows. This is where you set things specific to your interactive shell that aren't otherwise inherited from the parent process. For example, PS1 is set here because only interactive shells care about its value, and any interactive shell will source .bashrc anyway, so there is no need to define and export PS1 from .profile.


And though you didn't ask, it's worth pointing out the difference between .profile and .bash_profile here. .profile is "shared" by all POSIX shells (such as dash), so don't put anything bash-specific here. .bash_profile, though, is only used by bash, so you can use bash extensions in it. If .bash_profile is present, .profile will be ignored, so if for whatever reason you want to use both, you can add . ~/.profile to the top of your .bash_profile.


[#457] Saturday, April 22, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cugure

Total Points: 188
Total Questions: 110
Total Answers: 103

Location: Dominican Republic
Member since Sun, Sep 4, 2022
2 Years ago
;