How to Set Up Bash Aliases and Custom Shell Functions

Aliases and shell functions turn frequently-typed long commands into short, memorable shortcuts — a small investment that saves real time across daily VPS administration.

Creating a Simple Alias

alias ll='ls -lah'

Typed directly, this alias only lasts for the current shell session — add it to a startup file to make it permanent.

Making Aliases Permanent

nano ~/.bashrc
alias ll='ls -lah'
alias update='sudo apt update && sudo apt upgrade -y'
alias dcup='docker compose up -d'
alias dclogs='docker compose logs -f'
source ~/.bashrc

Useful Aliases for Common VPS Tasks

alias diskspace='df -h'
alias meminfo='free -h'
alias ports='sudo ss -tulnp'
alias myip='curl ifconfig.me'
alias reload='sudo systemctl daemon-reload'

Creating a Shell Function for More Complex Shortcuts

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

Unlike simple aliases, functions can accept arguments and include actual logic — here, creating and immediately entering a directory in one command.

A Function for Quick Backups Before Editing a Config File

backup() {
    cp "$1" "$1.bak.$(date +%Y%m%d%H%M%S)"
    echo "Backed up to $1.bak.$(date +%Y%m%d%H%M%S)"
}
backup /etc/nginx/nginx.conf

A Function to Quickly Find and Kill a Process by Name

killp() {
    ps aux | grep "$1" | grep -v grep | awk '{print $2}' | xargs -r kill
}

Organizing Aliases in a Separate File (Cleaner Approach)

nano ~/.bash_aliases
# Add aliases here
nano ~/.bashrc
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Keeps your main .bashrc cleaner and makes aliases easier to manage/version-control separately.

Sharing Aliases Across a Team

Consider committing a shared .bash_aliases file to a team dotfiles repository, so common shortcuts are consistent across everyone's environment on shared or personal servers.

Viewing All Currently Defined Aliases

alias

Removing an Alias for the Current Session

unalias ll

Common Errors

Alias doesn't persist after reconnecting — verify it was added to ~/.bashrc (not just typed interactively) and that you're using bash (not a different shell like zsh, which uses different configuration files).

Continue Reading

Browse more articles in Linux Server Administration.

  • bash aliases, shell functions, bashrc customization, linux productivity shortcuts
  • 0 משתמשים שמצאו מאמר זה מועיל
?האם התשובה שקיבלתם הייתה מועילה

מאמרים קשורים

How to Update and Upgrade Your Ubuntu or Debian VPS

Keeping your VPS updated is one of the most important and simplest maintenance tasks — it...

How to Enable and Configure Swap on a Linux VPS (Ubuntu & Debian)

Swap is disk space Linux can use as overflow memory when physical RAM is exhausted. It won't make...

How to Manage Services with systemd and systemctl

systemd is the service manager used by Ubuntu, Debian, and most modern Linux distributions to...

How to Read and Analyze Linux Logs with journalctl

On modern Ubuntu and Debian systems, journalctl is the central tool for viewing system and...

How to Schedule Tasks with Cron on a Linux VPS

Cron is the standard Linux job scheduler, used to automate recurring tasks like backups, log...