Bird
Raised Fist0
Linux CLIscripting~10 mins

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

Choose your learning style10 modes available
Concept Flow - .bashrc and .bash_profile
.bash_profile starts
↓
Is it a login shell?
No→Skip .bash_profile
Yes↓
Run commands in .bash_profile
↓
Does .bash_profile call .bashrc?
Yes→Run .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.

Practice

(1/5)
1. What is the primary purpose of the .bash_profile file in Linux?
easy
A. It stores command history for the shell.
B. It runs every time a new terminal window opens to set aliases.
C. It runs once at login to set environment variables.
D. It manages system-wide user permissions.

Solution

  1. Step 1: Understand the role of .bash_profile

    The .bash_profile file is executed once when a user logs in to the system. It is used to set environment variables and run commands that should only happen once per login session.
  2. Step 2: Differentiate from .bashrc

    The .bashrc file runs every time a new terminal is opened, mainly for setting aliases and shell options, not environment variables at login.
  3. Final Answer:

    It runs once at login to set environment variables. -> Option C
  4. Quick Check:

    .bash_profile = runs once at login [OK]
Hint: Remember: .bash_profile runs once at login [OK]
Common Mistakes:
  • Confusing .bash_profile with .bashrc
  • Thinking .bash_profile runs for every new terminal
  • Assuming .bash_profile manages command history
2. Which of the following is the correct way to load .bashrc from .bash_profile?
easy
A. execute ~/.bashrc
B. load ~/.bashrc
C. run ~/.bashrc
D. source ~/.bashrc

Solution

  1. Step 1: Identify the command to run another script

    In bash, the source command runs commands from a file in the current shell environment.
  2. Step 2: Confirm syntax for loading .bashrc

    Using source ~/.bashrc inside .bash_profile loads .bashrc settings for login shells.
  3. Final Answer:

    source ~/.bashrc -> Option D
  4. Quick Check:

    Use source to load scripts [OK]
Hint: Use 'source' to run .bashrc inside .bash_profile [OK]
Common Mistakes:
  • Using non-existent commands like load or run
  • Trying to execute .bashrc as a separate process
  • Forgetting to use the tilde (~) for home directory
3. Given the following .bash_profile content:
export PATH="$PATH:/custom/bin"
source ~/.bashrc

And .bashrc contains:
alias ll='ls -l'

What happens when you open a new terminal after login?
medium
A. The alias ll is set but PATH does not include /custom/bin.
B. The PATH includes /custom/bin and alias ll is available.
C. The PATH is unchanged and alias ll is not available.
D. Only the PATH is updated; alias ll is not set.

Solution

  1. Step 1: Understand .bash_profile execution at login

    The .bash_profile runs once at login, setting PATH to include /custom/bin and then sourcing .bashrc.
  2. Step 2: Understand .bashrc execution on new terminals

    Since .bashrc is sourced by .bash_profile, the alias ll='ls -l' is set for the shell session.
  3. Final Answer:

    The PATH includes /custom/bin and alias ll is available. -> Option B
  4. Quick Check:

    .bash_profile sets PATH + sources .bashrc = alias set [OK]
Hint: Sourcing .bashrc in .bash_profile applies aliases at login [OK]
Common Mistakes:
  • Assuming alias is not set without opening a new terminal
  • Thinking PATH changes only after logout/login
  • Ignoring that .bashrc is sourced inside .bash_profile
4. You added source ~/.bashrc to your .bash_profile, but aliases from .bashrc are still not available after login. What is the most likely problem?
medium
A. You forgot to log out and log back in after editing .bash_profile.
B. The .bashrc file has incorrect permissions and cannot be read.
C. The source command is misspelled in .bash_profile.
D. Aliases must be defined in .bash_profile, not .bashrc.

Solution

  1. Step 1: Check if changes require re-login

    Changes to .bash_profile only take effect after a new login session, so you must log out and back in.
  2. Step 2: Verify other options

    Incorrect permissions would cause errors, misspelling source would cause errors, and aliases are correctly placed in .bashrc.
  3. Final Answer:

    You forgot to log out and log back in after editing .bash_profile. -> Option A
  4. Quick Check:

    Changes in .bash_profile need re-login [OK]
Hint: Log out and back in to apply .bash_profile changes [OK]
Common Mistakes:
  • Not logging out after editing .bash_profile
  • Assuming aliases must be in .bash_profile
  • Ignoring file permission issues
5. You want to ensure that environment variables set in .bash_profile and aliases in .bashrc are available in all interactive shells, including login and non-login shells. Which is the best approach?
hard
A. Add environment variables to .bash_profile and add source ~/.bashrc inside .bash_profile only.
B. Add source ~/.bashrc inside .bash_profile and add source ~/.bash_profile inside .bashrc.
C. Add source ~/.bashrc inside .bash_profile and add environment variables to .bashrc.
D. Add all environment variables and aliases only in .bashrc.

Solution

  1. Step 1: Understand login vs non-login shells

    .bash_profile runs for login shells; .bashrc runs for interactive non-login shells.
  2. Step 2: Best practice to share settings

    Put environment variables in .bash_profile and source .bashrc from .bash_profile to load aliases for login shells. Non-login shells read .bashrc directly.
  3. Step 3: Avoid circular sourcing

    Do not source .bash_profile from .bashrc to prevent loops.
  4. Final Answer:

    Add environment variables to .bash_profile and add source ~/.bashrc inside .bash_profile only. -> Option A
  5. Quick Check:

    Env vars in .bash_profile + source .bashrc there = all shells get settings [OK]
Hint: Source .bashrc from .bash_profile; keep env vars in .bash_profile [OK]
Common Mistakes:
  • Sourcing .bash_profile inside .bashrc causing loops
  • Putting environment variables only in .bashrc
  • Not sourcing .bashrc in .bash_profile
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard