Based on the Linux Master 2 exam syllabusReport an error
/etc/) and user (~/). The global one is read first and then overridden by the user's./etc/profile → ~/.bash_profile, in that order.~/.bashrc.~/.bash_logout is executed.source or .. Global (all users) User (me only)
┌──────────────────┐ ┌──────────────────┐
Login │ /etc/profile │ → │ ~/.bash_profile │
└──────────────────┘ └──────────────────┘
Non-login│ /etc/bashrc │ → │ ~/.bashrc │
└──────────────────┘ └──────────────────┘
Logout ~/.bash_logout| Axis | What it distinguishes |
|---|---|
| Global vs user | Under /etc/ applies to everyone, under ~/ applies only to that person |
| Login vs non-login | Did you go through the login process, or did you just launch a new shell while already logged in? |
| File | Scope | When |
|---|---|---|
/etc/profile | Global | At login |
/etc/bashrc (/etc/bash.bashrc) | Global | When a shell is newly launched |
~/.bash_profile | Personal | At login |
~/.bash_login, ~/.profile | Personal | Used instead when .bash_profile does not exist |
~/.bashrc | Personal | When a shell is newly launched |
~/.bash_logout | Personal | At logout |
File names differ slightly between distributions. The global bashrc is
/etc/bashrcon the Red Hat family and/etc/bash.bashrcon the Debian family.
| What to write | Where to put it | Why |
|---|---|---|
Environment variables (PATH, LANG) | .bash_profile | Set once and they are passed on to children |
Aliases (alias), prompt | .bashrc | Needed fresh in every shell |
export, so once at login is enough.# ~/.bash_profile (default contents on the Red Hat family)
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH# ~/.bashrc (default contents on the Red Hat family plus one alias line)
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias ll='ls -l'[ -f file ] means "if that file exists", and . is source. So it means "if .bashrc exists, read it in the current shell.".bash_profile calls .bashrc, and .bashrc calls /etc/bashrc. It is not that bash reads everything on its own; one line written inside each file calls the next file.~/.profile by default instead of ~/.bash_profile, and call ~/.bashrc from inside it. The global /etc/bash.bashrc is read directly by bash. The way the files are connected differs by distribution, but the skeleton, "the login file calls bashrc," is the same./etc/profile.d/ — Where Global Settings Are Split Upls /etc/profile.d/
# colorls.sh lang.sh less.sh vim.sh which2.sh/etc/profile reads every *.sh file in this directory./etc/profile directly, a package drops one file of its own here. To remove it, you only delete that file..sh file here than to edit /etc/profile./etc/skel — Where a New Account's Configuration Files Come Fromls -a /etc/skel
# . .. .bash_logout .bash_profile .bashrcuseradd, the files in this directory are copied to the new home directory. skel is short for skeleton.This applies to console login, SSH connections, and su -.
1. /etc/profile global
│
└─► /etc/profile.d/*.sh (settings may be split up here)
2. ~/.bash_profile personal
│
└─► ~/.bashrc (usually called from here)
│
└─► /etc/bashrc~/.bash_profile does not exist it reads ~/.bash_login, and if that does not exist either, ~/.profile. It reads only the first one of the three that it finds..bashrc inside .bash_profile. That is what makes aliases apply in login shells too.This applies when you open a new terminal window or run bash while already logged in.
~/.bashrc
│
└─► /etc/bashrc/etc/profile and ~/.bash_profile are not read..bash_profile are used by inheriting them from the parent shell.| Situation | Is it a login shell? |
|---|---|
| SSH connection | Login shell |
| Console login | Login shell |
su - | Login shell |
su (without hyphen) | Non-login |
| Opening a new terminal window | Non-login |
Running bash | Non-login |
This is where the difference between
suandsu -shows. Adding the hyphen starts a login shell and reads from/etc/profileall over again, so the environment switches completely.
echo $0
# -bash ← a leading hyphen means a login shell
shopt login_shell
# login_shell on
bash # launch one more shell
echo $0
# bash
shopt login_shell
# login_shell offPut echo ">> .bash_profile" on the first line of ~/.bash_profile and echo ">> .bashrc" on the first line of ~/.bashrc, then try the following.
| What you did | What is printed on screen | Files read |
|---|---|---|
| New SSH connection | >> .bash_profile followed by >> .bashrc | Both (profile called bashrc) |
Run bash inside it | >> .bashrc only | bashrc only |
Run a script with bash script.sh | Nothing | Neither is read |
.bashrc either. That is why an alias defined in .bashrc does not work inside a script.scp may fail.su and su - Side by Side[user@server1 work]$ su
Password:
[root@server1 work]# pwd
/home/user/work ← still in the same place
[root@server1 work]# exit
[user@server1 work]$ su -
Password:
[root@server1 ~]# pwd
/root ← moved to root's home, and the environment is root's toosu changes only the user and leaves the working directory and most existing environment variables as they are.su - creates the same state as a fresh login as root. It reads /etc/profile and root's ~/.bash_profile from the beginning.su - alice, you enter that user's login shell.source ~/.bashrc # re-read in the current shell
. ~/.bashrc # a single dot means the same thingsource and . (dot) are the same command.source.bash ~/.bashrc. It would be read in a new shell and finish, leaving nothing in the current shell. The key point is that source runs inside the current shell.| Notation | Meaning |
|---|---|
. ~/.bashrc | There is a space after the dot — same as source. Reads in the current shell |
./script.sh | The dot and slash are joined — executes "that file in the current directory" |
source only reads the file, so it works even without execute permission. Running with ./script.sh requires execute permission..bash_profile, it is source ~/.bash_profile.# when ~/.bashrc contains export PATH=$PATH:/opt/bin
source ~/.bashrc
source ~/.bashrc
echo $PATH
# /usr/local/bin:/usr/bin:/opt/bin:/opt/bin:/opt/bin⚠️ Every time you
source, that line runs again. A line that appends to$PATHadds the same path once more each time it is read. It does no harm to how things work, but if you delete the line to get rid of the value and thensource, the values already appended do not disappear. In that case, log in again.
# ~/.bash_logout example
clear # clear the screen
rm -f ~/tmp/* # clean up temporary files| What you want to do | Where to edit |
|---|---|
Add a path to every user's PATH | /etc/profile (or add a .sh file to /etc/profile.d/) |
| Give every user the same alias | /etc/bashrc |
| An environment variable only I use | ~/.bash_profile |
| An alias or prompt only I use | ~/.bashrc |
| Default settings for accounts created in the future | Files under /etc/skel/ |
| Automatic cleanup at logout | ~/.bash_logout |
| Apply your edits right now | source file |
| Appears in related lists but is not a configuration file | Actual role |
|---|---|
~/.bash_history | The file where the history of executed commands accumulates |
/etc/shells | The list of shells that can be used |
/etc/passwd | Account information. The 7th field is the login shell |
Tomorrow we cover redirection, which sends a command's input and output elsewhere, as well as aliases and history.
/etc/profile and ~/.bash_profile.~/.bashrc./etc/profile : The global login configuration file that applies to all users.~/.bash_profile : A personal login configuration file. Mainly holds environment variables.~/.bashrc : A personal configuration file read every time a shell is launched. Mainly holds aliases and the prompt.~/.bash_logout : A file executed when logging out of a login shell.source : A command that re-reads a configuration file in the current shell to apply it immediately. Same as ../etc/profile.d/ : A directory that holds global login settings split into separate files. /etc/profile reads the *.sh files here./etc/skel : The place holding the default configuration files that are copied into the home directory when a new account is created.Click a choice to reveal the answer and explanation.
Question 1
Which of the following is the personal configuration file read when a non-login shell starts, such as when you open a new terminal window?
Question 2
Which of the following is the global login configuration file that applies to all users?
Question 3
Which of the following commands applies an edited configuration file to the current shell immediately, without logging in again?
Question 4
Which of the following configuration files is executed at logout?
Question 5
Which of the following files is read instead when ~/.bash_profile does not exist in a login shell?