0
0
Linux CLIscripting~10 mins

.bashrc and .bash_profile in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - .bashrc and .bash_profile
.bash_profile starts
Is it a login shell?
NoSkip .bash_profile
Yes
Run commands in .bash_profile
Does .bash_profile call .bashrc?
YesRun .bashrc
Shell ready for user
Open new terminal (non-login)
Run .bashrc only
Shell ready for user
When you start a login shell, .bash_profile runs first and may call .bashrc. For non-login shells, only .bashrc runs.
Execution Sample
Linux CLI
echo 'Hello from .bash_profile'
source ~/.bashrc

# .bashrc content:
echo 'Hello from .bashrc'
This simulates .bash_profile running first, then calling .bashrc, showing their outputs.
Execution Table
StepActionFileOutputNotes
1Start login shell.bash_profileHello from .bash_profile.bash_profile runs first
2Call .bashrc from .bash_profile.bashrcHello from .bashrc.bashrc runs inside .bash_profile
3Shell readyN/AUser can now use the shell
4Start non-login shell.bashrcHello from .bashrcOnly .bashrc runs
5Shell readyN/AUser can now use the shell
6ExitN/AShell session ends
💡 Shell session ends when user exits terminal
Variable Tracker
VariableStartAfter .bash_profileAfter .bashrcFinal
PS1 (prompt)defaultcustomized in .bash_profilefurther customized in .bashrcfinal prompt shown to user
PATHsystem defaultmodified in .bash_profilepossibly extended in .bashrcfinal PATH used by shell
Key Moments - 3 Insights
Why does .bashrc run when I open a new terminal but not .bash_profile?
Because .bash_profile runs only for login shells (see execution_table step 1 and 4). New terminals usually open non-login shells, so only .bashrc runs.
Why does .bash_profile often call .bashrc?
To reuse common settings for both login and non-login shells, .bash_profile sources .bashrc (see execution_table step 2). This avoids repeating code.
What happens if .bash_profile does not call .bashrc?
Then settings in .bashrc won't apply in login shells, causing inconsistent environment (see execution_table step 1 without step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which file runs first when you start a login shell?
A.bash_profile
B.bashrc
CNeither
DBoth at the same time
💡 Hint
Check step 1 in the execution_table where the login shell starts.
At which step does .bash_profile call .bashrc according to the execution_table?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look for the step mentioning calling .bashrc inside .bash_profile.
If you open a new terminal window (non-login shell), which file runs?
A.bash_profile only
B.bashrc only
CBoth .bash_profile and .bashrc
DNeither file
💡 Hint
See step 4 in the execution_table for non-login shell behavior.
Concept Snapshot
.bash_profile runs for login shells only.
.bashrc runs for interactive non-login shells.
.bash_profile often calls .bashrc to share settings.
Login shell = user logs in; non-login shell = new terminal window.
Use .bash_profile for login-only setup.
Use .bashrc for interactive shell setup.
Full Transcript
When you start a login shell, the system runs .bash_profile first. This file can set environment variables and run commands. Often, it calls .bashrc to apply common settings. When you open a new terminal window (a non-login shell), only .bashrc runs. This means .bash_profile is skipped. This separation helps organize settings: .bash_profile for login-specific setup, .bashrc for interactive shell setup. If .bash_profile does not call .bashrc, some settings may be missing in login shells. Understanding this flow helps you customize your shell environment correctly.