Weight: 4
Goal: Customize shell environments to meet users’ needs, and modify global and user profiles.
A shell is a program that takes commands you type and passes them to the operating system to run. The default shell on most Linux systems is Bash (Bourne Again Shell).
There are two kinds of shell sessions you need to know:
su -). It reads login-specific configuration
files.bash manually. It reads a different set of files.This distinction is the core of this objective — it determines which configuration files get read.
Configuration files set up your environment (variables, aliases, functions, prompt, etc.). There are global files (apply to all users) and user files (apply only to one user).
| File | When it runs |
|---|---|
/etc/profile |
Read once at login. Sets system-wide environment
variables (like PATH). |
/etc/bash.bashrc |
Read by non-login interactive Bash shells. (On Red
Hat-based systems this file is /etc/bashrc.) |
/etc/profile.d/ |
Directory of .sh scripts sourced by
/etc/profile. A clean way to add system-wide settings
without editing /etc/profile directly. |
~)Login shell reads, in this order, the first one that exists:
~/.bash_profile~/.bash_login~/.profileNon-login interactive shell reads:
~/.bashrcOn logout from a login shell:
~/.bash_logout is read (used for cleanup commands like
clearing the screen)..bashrc from
.bash_profileBecause login shells do not automatically read
~/.bashrc, most distributions put this snippet in
~/.bash_profile:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fiThis way, both login and non-login shells end up loading
~/.bashrc, and you only need to maintain settings in one
place.
Login shell:
/etc/profile → /etc/profile.d/*.sh → ~/.bash_profile (or ~/.bash_login, or ~/.profile)
│
└─► usually sources ~/.bashrc
│
└─► usually sources /etc/bashrc
Non-login interactive shell:
/etc/bash.bashrc → ~/.bashrc
Logout of login shell:
~/.bash_logout
source and .When you edit a config file, the changes do not apply to your current shell until you reload it. You reload by sourcing the file.
source ~/.bashrc
# or, equivalently:
. ~/.bashrc. (a single dot) and source do
exactly the same thing../script.sh) runs it in a
child shell, and changes vanish when the child
exits.Remember for the exam: . is not the
same as ./. The dot alone (followed by a space and a
filename) means “source this file.”
Bash has two kinds of variables:
MYVAR="hello" # shell variable
echo $MYVAR # → hello
export MYVAR # promote to environment variable
export OTHER="world" # create and export in one stepset, env,
export, unset| Command | What it does |
|---|---|
set |
Lists all shell variables and functions (and changes shell options). |
env |
Lists only environment variables. Can also run a command with a modified environment. |
export VAR |
Marks VAR as an environment variable so child processes
inherit it. export -p lists exported variables. |
unset VAR |
Removes a variable (works for both shell and environment variables). |
Example:
$ FOO=bar
$ echo $FOO
bar
$ bash # start a child shell
$ echo $FOO # empty — child can't see it
$ exit
$ export FOO
$ bash
$ echo $FOO # now → bar| Variable | Meaning |
|---|---|
PATH |
Colon-separated list of directories to search for commands. |
HOME |
The current user’s home directory. |
USER / LOGNAME |
Current username. |
SHELL |
Path to the user’s login shell. |
PWD |
Current working directory. |
PS1 |
Primary prompt string. |
LANG / LC_* |
Language and locale settings. |
EDITOR |
Default text editor for other programs. |
PATH VariablePATH tells the shell where to look for executables.
Example:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binWhen you type ls, the shell looks in each of those
directories (left to right) and runs the first ls it
finds.
export PATH="$PATH:/opt/myapp/bin" # append
export PATH="/opt/myapp/bin:$PATH" # prepend (searched first)Put this line in ~/.bash_profile (or
~/.bashrc) to make it permanent.
If a program is not in any PATH directory, you must give
its location explicitly:
./myscript.sh # in the current directory
/home/alice/tool # absolute pathThe leading ./ is required because, for security, the
current directory is not in PATH by
default.
An alias is a short name for a longer command.
alias ll='ls -l --color=auto'
alias rm='rm -i' # always ask before deleting
alias ..='cd ..'alias with no arguments lists all current aliases.unalias NAME removes one. unalias -a
removes them all.~/.bashrc.\rm file.txt.A function is a reusable group of commands. Use it when an alias is not enough (for example, when you need parameters or logic).
# Form 1 (POSIX-compatible)
greet() {
echo "Hello, $1"
}
# Form 2 (Bash-specific)
function greet {
echo "Hello, $1"
}$ greet Alice
Hello, Alice$1, $2, … are the function’s
arguments.$# is the number of arguments.$@ is all arguments.~/.bashrc to keep them available in
every shell.declare -f # list all defined functions
declare -f greet # show the body of one function
unset -f greet # remove the function/etc/skel/When you create a new user with useradd -m, the contents
of /etc/skel/ are copied into the new
user’s home directory. This is how every new user gets a default
~/.bashrc, ~/.bash_profile, etc.
$ ls -A /etc/skel/
.bash_logout .bashrc .profileTo give every new user a custom default (for example, a company-wide
alias), edit or add files in /etc/skel/. This affects
future users only — existing users are unaffected.
type — show what a
command is$ type ls
ls is aliased to 'ls --color=auto'
$ type cd
cd is a shell builtin
$ type bash
bash is /usr/bin/bashPossible categories: alias, keyword, function, builtin, file.
which — find an
executable in PATH$ which python3
/usr/bin/python3which only finds executables on disk; it does not see
aliases, functions, or builtins. Use type for those.
echo — print
text or variable values$ echo "User is $USER"
User is aliceFiles (memorize these):
. (dot — sources a file)source (same as .)/etc/bash.bashrc/etc/profile~/.bash_profile~/.bash_login~/.profile~/.bashrc~/.bash_logout/etc/skel/Commands and keywords:
envexportsetunsetfunctionaliasWhich file is read first when a user logs in:
~/.bash_profile, ~/.bash_login, or
~/.profile? ~/.bash_profile. The
shell reads the first one that exists in that order,
and stops there.
What is the difference between set and
env? set lists all shell variables,
functions, and shell options. env lists only exported
(environment) variables.
What does source ~/.bashrc do, and what is
its short form? It reads and executes ~/.bashrc in
the current shell. Short form: . ~/.bashrc.
You add alias ll='ls -l' to
~/.bashrc. Why doesn’t it work in your current
terminal? The file has not been re-read. Run
source ~/.bashrc or open a new shell.
Where do you place files that should appear in every new
user’s home directory? /etc/skel/.
How do you make PATH permanently include
/opt/bin for one user? Add
export PATH="$PATH:/opt/bin" to
~/.bash_profile (or ~/.bashrc).
How do you write a function that prints its first argument?
show() { echo "$1"; }What does unset FOO do? Removes the
variable FOO from the shell (whether or not it was
exported).