Aliases and functions: customize your terminal once, benefit daily

Stop retyping repetitive Linux commands. Learn how to write clean, reload-safe Bash aliases and functions in your .bashrc to supercharge your daily workflow.

By Asahluma Tyika

Stop Retyping the Same Five Commands

If you open your terminal every morning and type the same sequence of commands—checking git branches, hopping through three nested directories, or remembering the obscure flags for listing files—you are burning focus on manual labor.

Your shell is programmable. By configuring your ~/.bashrc file, you can turn a 40-character command into a two-letter shortcut or build a mini-tool that handles repetitive chores automatically.

This guide is for developers who know basic command-line commands (cd, ls, git) and want to work faster without memorizing dozens of obscure utility flags. We will cover the difference between aliases and functions, establish a reload-safe setup that keeps your terminal clean, and give you practical recipes you can use immediately.


Part 1: Aliases vs. Functions

Before writing code, you need to know which tool fits the job. Bash gives you two main ways to create shortcuts: aliases and functions.

Aliases: Simple Word Replacement

An alias is a string replacement. When you type the alias, Bash swaps it for the command string you defined before running it.

alias gs='git status -sb'

When you type gs, Bash actually executes git status -sb. Aliases are ideal for shortening static commands and adding default flags to existing tools.

Functions: Real Scripting Power

Aliases fall short when you need logic, positional arguments, or multiple steps. For example, an alias cannot take an argument and drop it into the middle of a command.

A Bash function works like a function in any programming language: it accepts parameters ($1, $2), can perform checks, and runs multiple commands in sequence:

mkcd() {
  mkdir -p "$1" && cd "$1"
}

Running mkcd project-alpha creates the directory and immediately navigates inside it.

Quick Comparison

Feature Alias Function
Primary Use Shortening static commands Multi-step tasks and logic
Accepts Arguments Only appends to the end Yes, anywhere ($1, $@)
Conditional Logic (if/else) No Yes
Complexity 1 line Multi-line scripts
Execution Speed Negligible overhead Negligible overhead

Part 2: The Reload-Safe Pattern

Whenever you edit your configuration, you have to tell Bash to reload it. Most people do this:

source ~/.bashrc

However, if your configuration isn’t built carefully, sourcing ~/.bashrc repeatedly during a session can cause problems:

  1. $PATH grows longer on every reload with duplicate entries.
  2. Stale functions remain in memory even if you renamed them in the file.
  3. Your configuration file grows messy when you mix terminal tweaks, shortcuts, and custom scripts.

Here is the clean, reload-safe architecture to fix that.

1. Separate Shortcuts from Core Settings

Keep your aliases and functions in a dedicated file named ~/.bash_aliases. Then, ensure your ~/.bashrc loads it automatically.

Open ~/.bashrc in your text editor and confirm this snippet is present:

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

(Note: In Bash, . is shorthand for source.)

2. Make Path Additions Idempotent

If your configuration updates your $PATH, wrap it in a check so reloads do not append the same folder over and over:

# Add a directory to PATH only if it isn't already there
add_to_path() {
  case ":$PATH:" in
    *":$1:"*) ;;
    *) export PATH="$1:$PATH" ;;
  esac
}

add_to_path "$HOME/.local/bin"

3. Build a Dedicated Reload Alias

Add this alias inside ~/.bash_aliases:

alias reload='source ~/.bashrc && echo "Config reloaded successfully."'

Now, every time you update your shortcuts, you only need to type reload.


Part 3: Practical Recipes to Steal

Here is a curated collection of daily-use aliases and functions.

Moving across directories takes too many keystrokes. These speed up common traversals:

# Directory traversal
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'

# List files with human-readable sizes and color
alias ll='ls -lah --color=auto'

Git Helpers

Git commands get repetitive fast. These aliases keep you in flow:

alias glog='git log --oneline --graph --decorate -n 15'
alias gca='git commit -am'
alias gco='git checkout'
alias gup='git pull --rebase'

Safety Nets

Accidentally overwriting a file with cp or deleting items with rm can be painful. You can turn on interactive confirmations:

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Smart Utility Functions

These three functions solve common developer bottlenecks:

# 1. Create a directory and cd into it immediately
mkcd() {
  if [ -z "$1" ]; then
    echo "Usage: mkcd <directory_name>"
    return 1
  fi
  mkdir -p "$1" && cd "$1"
}

# 2. Check what process is listening on a specific local port
port() {
  if [ -z "$1" ]; then
    echo "Usage: port <port_number>"
    return 1
  fi
  lsof -i :"$1"
}

# 3. Create a quick timestamped backup of any file
bak() {
  if [ ! -f "$1" ]; then
    echo "Error: '$1' is not a valid file."
    return 1
  fi
  cp "$1" "${1}.bak-$(date +%Y%m%d%H%M%S)" && echo "Created ${1}.bak-$(date +%Y%m%d%H%M%S)"
}

Hands-On Example

Let’s set up a complete, working ~/.bash_aliases file from scratch and verify everything in under two minutes.

Step 1: Create or Overwrite ~/.bash_aliases

Run this command directly in your terminal to write out a starter file:

cat << 'EOF' > ~/.bash_aliases
# --- Reload shortcut ---
alias reload='source ~/.bashrc && echo "Config reloaded."'

# --- Navigation & Listing ---
alias ..='cd ..'
alias ll='ls -lah --color=auto'

# --- Git ---
alias gs='git status -sb'

# --- Functions ---
mkcd() {
  if [ -z "$1" ]; then
    echo "Usage: mkcd <directory>"
    return 1
  fi
  mkdir -p "$1" && cd "$1"
}
EOF

Step 2: Source the New Configuration

Load the file into your active terminal:

source ~/.bashrc

Step 3: Test the Shortcuts

Test the mkcd function and ll alias together:

mkcd test-directory
ll

You should see your terminal create test-directory, navigate inside it, and display an empty directory listing with permissions and hidden files shown. Clean up when you’re done:

cd ..
rmdir test-directory

Common Mistakes / Troubleshooting

  • Putting spaces around the equals sign in an alias:
    Wrong: alias gs = 'git status'
    Fix: Bash treats spaces as argument separators. Write alias gs='git status' with no spaces around =.

  • Trying to use positional arguments inside an alias:
    Wrong: alias search='grep "$1" ./log.txt'
    Fix: Aliases ignore variables like $1. Use a function whenever you need to process input arguments.

  • Shadowing critical commands without fallback:
    If you alias cd or python to a custom wrapper that fails, standard scripts may break. If you ever need to bypass an alias temporarily, prefix the command with a backslash: \cd or \ls.

  • Changes not appearing in new tabs:
    If your aliases work in the current terminal after typing reload, but disappear when you open a new tab, your ~/.bashrc is not sourcing ~/.bash_aliases. Ensure the snippet from Part 2 is present in ~/.bashrc.


Try It Yourself

Write a custom function named touchx that creates a new file and makes it immediately executable with chmod +x.

Requirements:

  1. Check that an argument was provided (print an error and exit the function if missing).
  2. Create the file using touch.
  3. Apply executable permissions to the user using chmod +x.
  4. Test it by running touchx script.sh and inspecting the file permissions with ls -l script.sh.

What’s Next

Customizing your command-line environment changes how you interact with your operating system. Once your daily shortcuts are in place, you can move on to deeper productivity tweaks:

  • Customizing $PS1: Build a clean, informative prompt that displays your active Git branch, dirty state, and current directory without slowing down your terminal.
  • Synchronizing your dotfiles: Place your ~/.bash_aliases into a private Git repository and use symlinks to sync your shortcuts across every machine you touch.
  • Adding tab-completion: Write Bash auto-complete scripts for your custom functions so your arguments auto-fill just like system commands.

Bookmark this page so you have a quick reference the next time you set up a fresh Linux environment.

Share: X (Twitter) Facebook LinkedIn