Chapter 14: Customize Linux Shell

In this chapter, we’ll explore advanced ways to customize your shell environment to make your workflow more efficient. You’ll learn how to configure your shell using .bashrc, create reusable functions, customize your shell prompt, and automate tasks with aliases and scripts. By the end of this chapter, you’ll have a highly personalized and productive shell setup.


1. The .bashrc File

The .bashrc file is executed every time you start a new shell session. It’s the perfect place to store environment variables, aliases, and functions.

Editing .bashrc

Open the .bashrc file in your favorite text editor:

$ nano ~/.bashrc

Common Customizations

  • Set Environment Variables:
    $ export EDITOR=nano export PATH=$PATH:/usr/local/bin
  • Add Aliases:
    $ alias ll='ls -la' alias cls='clear'
  • Define Functions:
    $ function mkcd() { mkdir -p “$1" cd "$1" }

Reload .bashrc

After making changes, reload the file to apply them:

$ source ~/.bashrc

2. The .bash_aliases File

The .bash_aliases file is a convenient place to store all your aliases. It’s typically sourced from .bashrc.

Creating .bash_aliases

If the file doesn’t exist, create it:

$ touch ~/.bash_aliases

Adding Aliases

Add your aliases to .bash_aliases:

alias ll='ls -la'
alias gs='git status'
alias gp='git pull'

Sourcing .bash_aliases

Ensure .bash_aliases is sourced in your .bashrc:

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

3. Customizing the Shell Prompt

The shell prompt (PS1) can be customized to display useful information like the current directory, username, and hostname.

Basic Customization

Edit your .bashrc to customize PS1:

$ export PS1="\u@\h:\w\$ "
  • \u: Username.
  • \h: Hostname.
  • \w: Current working directory.
  • \$: Displays # for root and $ for other users.

Adding Colors

Use ANSI escape codes to add colors:

$ export PS1="\[\e[32m\]\u@\h:\w\$ \[\e[0m\]"
  • \[\e[32m\]: Start green text.
  • \[\e[0m\]: Reset to default color.

Example Prompt

A colorful prompt with the current directory:

$ export PS1="\[\e[32m\]\u@\h:\[\e[34m\]\w\[\e[32m\]\$ \[\e[0m\]"

4. Creating Custom Functions

Shell functions allow you to encapsulate reusable blocks of code.

Example: Backup Function

Create a function to back up a file:

function backup() {
  cp "$1" "$1.bak"
  echo "Backup created: $1.bak"
}

Usage:

$ backup important_file.txt

Example: Directory Navigation

Create a function to quickly navigate to a project directory:

function proj() {
  cd ~/projects/"$1"
}

Usage:

$ proj my_project

5. Using .inputrc for Readline Customization

The .inputrc file allows you to customize the behavior of the Readline library, which is used by Bash for command-line editing.

Example: Case-Insensitive Tab Completion

Add the following to ~/.inputrc:

$ set completion-ignore-case on

Reload the settings:

$ bind -f ~/.inputrc

6. Practical Examples

Customize Your Prompt

Edit your .bashrc to create a colorful prompt:

export PS1="\[\e[32m\]\u@\h:\[\e[34m\]\w\[\e[32m\]\$ \[\e[0m\]"

Create a Backup Alias

Add an alias to .bash_aliases:

alias backup='cp $1 $1.bak'

Define a Function for Git Shortcuts

Add a function to .bashrc:

function gits() {
  git status
  git log -n 1
}

Extra: Shell Customization Tools

Customizing your shell can greatly enhance your command-line experience. Frameworks like Oh My Zsh and Oh My Fish simplify the installation of themes and plugins, which can improve readability and provide context-specific information. 

For script writers, shellcheck is an invaluable tool for identifying common pitfalls and syntax errors. Remember, shell customization is highly personal; exploring different themes and plugins can help you find a setup that aligns with your workflow, making the command line a more inviting and productive space.

Customize your shell with Oh My Zsh:

$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Debug scripts with shellcheck:

$ shellcheck script.sh

Practice Time!

Let’s put your new skills to the test: 
1. Edit your .bashrcto customize your shell prompt. 
2. Create an alias for a frequently used command. 
3. Write a function to automate a repetitive task. 
4. Use .inputrc to enable case-insensitive tab completion.


That’s it for Chapter 14! You’ve now learned how to customize your shell environment with scripts, functions, and advanced configuration. In the next chapter, we’ll dive into writing your first script—a step-by-step guide to creating a useful shell script. Until then, experiment with shell customization to make your Linux experience even more efficient.


Prev: Chapter 13 | Next: Chapter 15