105.1 Customize and Use the Shell Environment

Weight: 4

Goal: Customize shell environments to meet users’ needs, and modify global and user profiles.


1. What is a Shell?

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:

This distinction is the core of this objective — it determines which configuration files get read.


2. Shell Configuration Files

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).

Global configuration files

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.

User configuration files (in the user’s home directory ~)

Login shell reads, in this order, the first one that exists:

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.profile

Non-login interactive shell reads:

On logout from a login shell:

The common trick: sourcing .bashrc from .bash_profile

Because login shells do not automatically read ~/.bashrc, most distributions put this snippet in ~/.bash_profile:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

This way, both login and non-login shells end up loading ~/.bashrc, and you only need to maintain settings in one place.

Summary diagram of load order

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

3. Sourcing Files: 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

Remember for the exam: . is not the same as ./. The dot alone (followed by a space and a filename) means “source this file.”


4. Environment Variables vs. Shell Variables

Bash has two kinds of variables:

Creating and using variables

MYVAR="hello"            # shell variable
echo $MYVAR              # → hello
export MYVAR             # promote to environment variable
export OTHER="world"     # create and export in one step

set, 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

Important environment variables

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.

5. The PATH Variable

PATH tells the shell where to look for executables. Example:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

When you type ls, the shell looks in each of those directories (left to right) and runs the first ls it finds.

Adding a directory to PATH

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.

Running a command outside PATH

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 path

The leading ./ is required because, for security, the current directory is not in PATH by default.


6. Aliases

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 ..'

7. Functions

A function is a reusable group of commands. Use it when an alias is not enough (for example, when you need parameters or logic).

Two equivalent syntaxes

# Form 1 (POSIX-compatible)
greet() {
    echo "Hello, $1"
}

# Form 2 (Bash-specific)
function greet {
    echo "Hello, $1"
}

Using a function

$ greet Alice
Hello, Alice

Listing and removing functions

declare -f              # list all defined functions
declare -f greet        # show the body of one function
unset -f greet          # remove the function

8. Skeleton Directory: /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  .profile

To 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.


9. Useful Commands for This Objective

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/bash

Possible categories: alias, keyword, function, builtin, file.

which — find an executable in PATH

$ which python3
/usr/bin/python3

which 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 alice

10. Quick Reference: Files and Commands for the Exam

Files (memorize these):

Commands and keywords:


11. Likely Exam Questions (Self-Check)

  1. Which 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.

  2. What is the difference between set and env? set lists all shell variables, functions, and shell options. env lists only exported (environment) variables.

  3. What does source ~/.bashrc do, and what is its short form? It reads and executes ~/.bashrc in the current shell. Short form: . ~/.bashrc.

  4. 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.

  5. Where do you place files that should appear in every new user’s home directory? /etc/skel/.

  6. How do you make PATH permanently include /opt/bin for one user? Add export PATH="$PATH:/opt/bin" to ~/.bash_profile (or ~/.bashrc).

  7. How do you write a function that prints its first argument?

    show() { echo "$1"; }
  8. What does unset FOO do? Removes the variable FOO from the shell (whether or not it was exported).