Great question! Understanding where to put environment variable exports and aliases can help ensure they work as expected in your shell sessions.
~/.bash_profile vs ~/.bashrc
- ~/.bash_profile is executed only for login shells (e.g., when you log in via text console, SSH, or start a terminal emulator configured to run a login shell).
- ~/.bashrc is executed for interactive non-login shells (e.g., when you open a new terminal window inside a graphical environment like GNOME or KDE).
Where to export environment variables like $GDK_DPI_SCALE or $LESSCHARSET?
These are environment variables that should be set once per login session and inherited by all child processes, so:
- It's usually best to export these in ~/.bash_profile or ~/.profile because these files run during login, setting the environment for the whole session.
- However, if your terminal emulator doesn't behave like a login shell (very common), sometimes they won't be set if only put in
.bash_profile.
- To cover more cases, some people put exports in
.bashrc and then source .bashrc from .bash_profile, or put them in .profile, since some distributions source .profile from .bash_profile.
Where to put aliases?
- Aliases are usually interactive conveniences and should be available in every interactive shell.
- Therefore, place aliases in ~/.bashrc.
- You can ensure
.bash_profile sources .bashrc so aliases are available in login shells too:
# In ~/.bash_profile
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
Summary
| Item | Typical location | Reason |
|---|
Environment variables (e.g., GDK_DPI_SCALE) | ~/.bash_profile (or ~/.profile) | Set once per login session for all programs |
| Aliases | ~/.bashrc | Available in all interactive shells |
Additional notes
- Some Linux distributions (e.g., Ubuntu) use
.profile instead of .bash_profile.
- If you want variables available in graphical desktop environment applications started from the GUI (not a shell), you need to configure those via your desktop environment's settings or place variables in files that the graphical environment reads, like
.xprofile or .pam_environment.
Example:
# ~/.bash_profile
export GDK_DPI_SCALE=1
export LESSCHARSET=utf-8
# Source bashrc for aliases and functions
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
# ~/.bashrc
alias ll='ls -lah'
alias gs='git status'
If you want me to help you set this up for your environment, just let me know!