Part 4: Environment Mastery - Customizing Your Shell
Table of Contents
Learn to customize your shell with environment variables, aliases, and the .bashrc file Welcome to Part 4! Now we’ll make the terminal truly yours by customizing your shell environment. Your shell is the program that interprets your commands. Most Linux systems use Bash (Bourne Again SHell), but others like Zsh and Fish are popular too. Check your shell: Exercise 4.1: Environment variables are like global settings for your shell. They store information that programs can access. Exercise 4.2: When you type Exercise 4.3: Temporary (current session only): Permanent (edit Exercise 4.4: The Location: Exercise 4.5: ⚠️ Important: Always backup before editing! Apply changes: Aliases are shortcuts for long commands. They make you faster and save typing. Temporary (current session): Permanent (add to Exercise 4.6: Open your Exercise 4.7: The prompt is the text before your cursor. By default: See your current prompt: Prompt escape codes: Exercise 4.8 - Simple custom prompt: Make it permanent - add to Now let’s do something cool! We’ll install and configure berzifetch - a system information tool written by a friend. berzifetch displays your system information in a beautiful format. Think of it like Repository: github.com/Spirizeon/berzifetch Exercise 4.9: Copy to a directory already in PATH: Let’s make it run every time you open a terminal! Exercise 4.11: Now every new terminal shows your system info! Functions are like aliases on steroids - they can accept arguments! Exercise 4.12 - Add these functions to Usage: Ready to practice? Complete the hands-on challenge in the workshop repository: Challenge: Customize your shell with useful aliases and functions to boost your productivity. The challenge includes: Clone the repository and give it a try: Different files load at different times: Best practice: Put everything in Navigation
Understanding Your Shell
echo $SHELL
# Output: /bin/bash (or /bin/zsh, etc.)echo $SHELL
bash --versionEnvironment Variables
Viewing Environment Variables
env # List all environment variables
echo $HOME # Your home directory
echo $USER # Your username
echo $PATH # Where the shell looks for commands
echo $PWD # Current directory
echo $SHELL # Your shell programenv | grep USER
echo $HOME
echo $PATHUnderstanding
$PATH$PATH is critical. It’s a colon-separated list of directories where the shell looks for commands.echo $PATH
# Output: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/gamesls, the shell searches these directories in order:/usr/local/bin/ls/usr/bin/ls ← Found! (stops searching)/bin/lsecho $PATH
which ls # Shows which 'ls' is being used
which python3Setting Environment Variables
export MY_VAR="Hello"
echo $MY_VAR
export PATH="$PATH:$HOME/bin" # Add to PATH.bashrc or .bash_profile):# We'll do this in the next section!export WORKSHOP="Linux CLI"
echo $WORKSHOP
echo "I'm learning $WORKSHOP"
# Try in a new terminal - it's gone! (temporary)Common Environment Variables
$HOME # Your home directory
$USER # Your username
$HOSTNAME # Computer name
$PWD # Current directory
$OLDPWD # Previous directory
$PATH # Command search path
$SHELL # Your shell
$EDITOR # Default text editor
$LANG # Language settings
$PS1 # Prompt stringThe
.bashrc File.bashrc file is a script that runs every time you open a new terminal. This is where you customize your shell!~/.bashrc (hidden file in your home directory)cd ~
ls -la | grep bashrc
cat ~/.bashrc | head -20Editing
.bashrccp ~/.bashrc ~/.bashrc.backup
nano ~/.bashrc # or: vim ~/.bashrc, code ~/.bashrcsource ~/.bashrc # Reload the file
# OR
. ~/.bashrc # Same thing (. is shorthand for source)
# OR
exec bash # Restart the shellAliases - Your Personal Shortcuts
Creating Aliases
alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'.bashrc):nano ~/.bashrc
# Add at the end:
# alias ll='ls -la'
# alias gs='git status'
# alias ..='cd ..'
source ~/.bashrc# Create temporary aliases
alias ll='ls -lah'
alias c='clear'
alias h='history'
# Try them
ll
c
hUseful Aliases to Add
.bashrc and add these:# Navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ~='cd ~'
# Listing
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'
# Safety
alias rm='rm -i' # Confirm before delete
alias cp='cp -i' # Confirm before overwrite
alias mv='mv -i' # Confirm before overwrite
# Git shortcuts
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
# System
alias df='df -h'
alias du='du -h'
alias free='free -h'
alias ports='netstat -tulanp'
# Quick edit
alias bashrc='nano ~/.bashrc'
alias reload='source ~/.bashrc'
# Miscellaneous
alias now='date +"%T"'
alias nowdate='date +"%d-%m-%Y"'
alias weather='curl wttr.in'# Add these to your .bashrc
nano ~/.bashrc
# Add at the end:
alias ll='ls -lah'
alias ..='cd ..'
alias gs='git status'
alias reload='source ~/.bashrc'
# Save (Ctrl+O, Enter, Ctrl+X in nano)
source ~/.bashrc
# Test
ll
..
reloadViewing and Removing Aliases
alias # List all aliases
alias ll # Show specific alias
unalias ll # Remove alias (temporary)Customizing Your Prompt (
$PS1)user@hostname:~$echo $PS1
# Output: \u@\h:\w\$\u - Username\h - Hostname\w - Full working directory\W - Current directory name only\$ - $ for normal user, # for root\d - Date\t - Time (24-hour)\n - Newline# Try these (temporary)
PS1="\u@\h:\w\$ " # Default
PS1="[\t] \u:\w\$ " # Add time
PS1="\w > " # Minimalist
PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ " # Colors!.bashrc:# Colorful prompt (green user@host, blue directory)
PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "The berzifetch Project
What is berzifetch?
neofetch but customizable!Installing berzifetch
# Clone the repository
cd
git clone https://github.com/Spirizeon/berzifetch.git
cd berzifetch
# Read the README
cat README.md
# Try it
./mainAdding berzifetch to Your PATH
sudo cp main /usr/local/bin/berzifetch
# Now you can run: berzifetch (from anywhere)
# Reload
source ~/.bashrc
# Verify
which berzifetch
berzifetch
Creating a berzifetch Alias
nano ~/.bashrc
# Add at the end:
alias fetch='~/berzifetch/main'
# OR make it auto-run on terminal startup:
~/berzifetch/main
# Save and reload
source ~/.bashrcFunctions in
.bashrc.bashrc:# Create a directory and enter it
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Extract any archive
extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.gz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.rar) unrar x "$1" ;;
*.tar) tar xf "$1" ;;
*) echo "Unknown archive format" ;;
esac
else
echo "File not found"
fi
}
# Quick backup
backup() {
cp "$1" "$1.backup-$(date +%Y%m%d-%H%M%S)"
}
# Find process by name
psgrep() {
ps aux | grep -v grep | grep -i -e VSZ -e "$1"
}source ~/.bashrc
mkcd test_folder # Creates and enters directory
backup important.txt # Creates timestamped backup
psgrep firefox # Find Firefox processesHands-On Challenge
git clone https://github.com/bakayu/linux-tutorial.git
cd linux-tutorial/04-environment-customization
cat README.mdAdvanced: Shell Configuration Files
~/.bash_profile # Login shells (SSH, first terminal)
~/.bashrc # Interactive shells (new terminal tabs)
~/.bash_logout # When you logout
~/.bash_aliases # Separate file for aliases (sourced by .bashrc).bashrc, then have .bash_profile source it:# ~/.bash_profile
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fiQuick Reference Card
# Environment Variables
export VAR="value" # Set variable
echo $VAR # Print variable
env # List all variables
echo $PATH # Command search path
# Configuration
nano ~/.bashrc # Edit config
source ~/.bashrc # Reload config
cp ~/.bashrc ~/backup # Backup
# Aliases
alias ll='ls -la' # Create alias
alias # List aliases
unalias ll # Remove alias
# Functions
function_name() { # Define function
commands
}
# PATH Management
export PATH="$PATH:/new/path" # Add to PATH
which command # Find command location