Choose the Right Shell Startup File
Choose the Right Shell Startup File
The class setting works in your current shell, but disappears when you start another session. To load a setting automatically, you can place appropriate commands in a startup file. The tricky part is choosing the file for the kind of shell session you actually start.
Bash does not read every startup file on every launch. Login shells, interactive non-login shells and non-interactive invocations have different rules. We will keep that distinction visible instead of treating .bashrc as a universal Linux configuration file.
What you’ll learn
- Choose a Bash startup file for the intended shell mode.
- Distinguish interactive non-login startup from login startup.
Different Bash modes read different startup files
Login shell
Bash reads login startup files under its documented selection rules.
profile familyInteractive non-login
This mode normally reads .bashrc.
.bashrcConfiguration choice
Place the setting for its intended mode rather than assuming every shell reads one file.
match file to mode
Choose where to save a Bash class variable
Prepare the example
The block below is content to put in the appropriate Bash startup file; running it once in your current shell does not edit that file. Inspect and back up the chosen file before modifying it. Use a test account or a recoverable session, because syntax errors in startup files can make later shells confusing.
Bash · Linux terminal
# Example content for an appropriate Bash startup file
export SCHOOL_CLASS=10AExpected behavior; machine-dependent details
A newly configured shell inherits the setting when it reads that file.The comment line is explanatory text for the file, and export SCHOOL_CLASS=10A both assigns and exports the class setting. Choose where it belongs based on the intended Bash invocation. An interactive non-login Bash normally reads .bashrc; a login Bash follows its profile selection rules.
Many configurations explicitly source another startup file, which can make the observed behavior look more universal than it really is. Trace those relationships rather than assume Bash reads all files itself. After editing, start the relevant kind of fresh session and inspect the setting. Keep output-producing commands and interactive behavior out of startup paths where they would disrupt automated work.
Match Bash configuration to its startup mode
Bash reads different startup files in different situations. Two common user files are ~/.bashrc for interactive Bash shells and ~/.profile for login-session environment setup on many systems.
1. What are .bashrc and .profile?
- .bashrc: This file runs every time you open a new terminal window. It’s perfect for adding aliases, prompt settings, or custom functions.
- .profile: This file runs at login for login shells. You can set environment variables here that you want available across the entire session.
2. Locate the Files
First, open your terminal and go to your home directory:
cd ~
Then, list the hidden files:
ls -la
-rw-r--r-- 1 user user 220 Jul 2 10:21 .bash_logout
-rw-r--r-- 1 user user 3771 Jul 2 10:21 .bashrc
-rw-r--r-- 1 user user 807 Jul 2 10:21 .profile
You'll see both .bashrc and .profile listed there.
3. Editing the .bashrc File
Let’s say you want to add a custom alias. You can open .bashrc with your favorite text editor. We'll use nano here:
nano ~/.bashrc
Scroll to the bottom and add a line like this:
alias ll='ls -la'
Save and exit:
- Press Ctrl + O to write changes
- Then Enter to confirm
- And Ctrl + X to exit nano
To apply the changes without restarting your terminal, run:
source ~/.bashrc
4. Editing the .profile File
Now let’s look at .profile. This is ideal for environment variables that need to persist across terminal sessions.
nano ~/.profile
For example, to add a new path to your environment:
export PATH="$PATH:$HOME/my-scripts"
Save and exit as before, then load the file:
source ~/.profile
5. When to Use Which File?
- Use
.bashrcfor things you want active in every terminal window (aliases, shell functions). - Use
.profilefor things that should be set once per session (PATH, LANG variables).
6. Pro Tip: Back Up Before Editing
It’s a good idea to back up these files before making changes:
cp ~/.bashrc ~/.bashrc.bak
cp ~/.profile ~/.profile.bak
7. Summary
That’s it! Now you know how to edit your Linux user profile and startup files to make your command-line experience truly yours. Try adding a few aliases or environment paths and see how much smoother things become.
Try it, then check the worked answer
Your task: Does every shell read .bashrc on every startup?
The block below is content to put in the appropriate Bash startup file; running it once in your current shell does not edit that file. Inspect and back up the chosen file before modifying it. Use a test account or a recoverable session, because syntax errors in startup files can make later shells confusing.
Bash · complete example for this question
# Example content for an appropriate Bash startup file
export SCHOOL_CLASS=10AExpected behavior; machine-dependent details
A newly configured shell inherits the setting when it reads that file.Worked answer
No; shell type and login/interactive mode determine startup behavior.
Not every shell reads .bashrc on every startup. The shell program and invocation mode determine the rules, and local files may explicitly source others. Identify your actual session before choosing the file you expect to load the setting.
Check your decision
The shell’s startup mode selects its files
Persistence needs an appropriate startup configuration, not just a one-time export. Match the file to the shell mode, inspect any sourcing relationships, and test a new intended session. Keep a recovery route available while changing configuration that future shells will execute.
Reaching the recap and completing the quizzes are separate milestones.