DEV Community

Cover image for Linux Aliases and Bash Functions: Work Smarter on the Command Line
Yeasin's
Yeasin's

Posted on AI-assisted

Linux Aliases and Bash Functions: Work Smarter on the Command Line

Every Linux user types the same commands over and over. ls -la to list files. cd into that one deep project folder. Update the system, then upgrade it. Each command is small — but together, they eat minutes from every single day.

The shell has two built-in tools that fix this: aliases for shortening commands, and functions for anything that needs logic. In this short guide, you'll learn both, when to use which, and how to make them permanent.


Aliases: Shortcut for Any Command

An alias is a simple text substitution. When you type the shortcut, the shell replaces it with the full command.

alias ll='ls -la'
Enter fullscreen mode Exit fullscreen mode

Now typing ll runs ls -la:

ll
Enter fullscreen mode Exit fullscreen mode
total 24K
drwxr-xr-x 3 yeasin yeasin 4.0K Oct 17 12:00 projects
-rw-r--r-- 1 yeasin yeasin 2.2K Oct 16 18:42 notes.txt
Enter fullscreen mode Exit fullscreen mode

A few that most people set up on day one:

alias ll='ls -la'
alias gs='git status'
alias up='sudo apt update && sudo apt upgrade -y'
alias ..='cd ..'
alias ports='ss -tlnp'
Enter fullscreen mode Exit fullscreen mode

Managing aliases

alias           # list all current aliases
type ll         # see what a specific alias expands to
unalias ll      # remove one alias
Enter fullscreen mode Exit fullscreen mode

Aliases can include arguments at the end

An alias simply appends whatever you type after it:

alias gs='git status'
gs --short       # runs: git status --short
Enter fullscreen mode Exit fullscreen mode

Safety aliases

Aliases can also protect you from yourself:

alias cp='cp -i'      # ask before overwriting
alias mv='mv -i'
alias rm='rm -i'      # ask before deleting
Enter fullscreen mode Exit fullscreen mode

Warning: These safety aliases create a habit of trusting rm to ask. But aliases only exist in interactive shells — inside scripts, on other servers, or as root, plain rm deletes silently. Treat the safety alias as a convenience, not a guarantee.


Bash Functions: Aliases With Logic

Aliases have a hard limit: they are pure text replacement. The moment you need arguments in the middle of a command, or any logic at all, you need a function.

mkcd() {
    mkdir -p "$1" && cd "$1" || return
}
Enter fullscreen mode Exit fullscreen mode

Usage:

mkcd projects/new-app
Enter fullscreen mode Exit fullscreen mode

This creates the directory (including parents) and enters it — one command instead of two. The "$1" is the function's first argument, quoted to survive spaces in names.

A few more genuinely useful examples:

# Extract any archive with one command
extract() {
    case "$1" in
        *.tar.gz) tar -xzf "$1" ;;
        *.tar.xz) tar -xJf "$1" ;;
        *.zip)    unzip "$1" ;;
        *) echo "Unknown archive type: $1" ;;
    esac
}

# Create a directory and cd into a project
goto() {
    cd ~/projects/"$1" 2>/dev/null || echo "No project: $1"
}

# Quick backup of a file
backup() {
    cp "$1" "$1.bak-$(date +%Y%m%d)"
}
Enter fullscreen mode Exit fullscreen mode

Now backup notes.txt produces notes.txt.bak-20261017 — and arguments land exactly where you put $1, which is the thing aliases can never do.


Alias vs Function: Which One?

The rule is simple:

Use When
Alias One command, fixed text, maybe arguments appended at the end
Function Arguments in the middle, multiple steps, if/else logic, variables

Quick test: if you can write it as shortcut='one command', it's an alias. The moment you need $1 somewhere inside the command, or more than one step, write a function.


Making Them Permanent

An alias or function typed in the terminal dies when the terminal closes. To keep them, put them in a shell startup file.

On Debian and Ubuntu, add them to ~/.bashrc:

nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Add your aliases and functions at the end of the file, save, then reload:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

source re-reads the file so your changes apply immediately — no new terminal needed.

Tip: As your collection grows, keep it organized in a separate file and load it from ~/.bashrc with one line:

# inside ~/.bashrc
[ -f ~/.bash_aliases ] && source ~/.bash_aliases

Many systems include this line by default — check your ~/.bashrc first. A dedicated aliases file is easier to back up, share between machines, and read later.


Common Mistakes

1. Forgetting to reload — You added the alias to ~/.bashrc but it doesn't work. Run source ~/.bashrc or open a new terminal.

2. Confusing zsh and bash — If you use zsh (or a framework like Oh My Zsh), your config lives in ~/.zshrc, not ~/.bashrc. The syntax in this article works the same.

3. Naming an alias over a real command — alias ls='ls -la' changes ls everywhere, including inside your muscle memory from tutorials. Safer: pick a fresh name like alias l='ls -la', or check for collisions first with type name.

4. Too many aliases — If you forget what a shortcut does, it costs more time than it saves. Keep alias (with no arguments) handy to review your list, and prune anything you don't use.


Summary

  • Aliases are text shortcuts — perfect for one fixed command with optional trailing arguments.
  • Functions handle arguments anywhere, multiple steps, and logic like if/else.
  • Rule of thumb: can't be one line with $1 in the middle? Write a function.
  • Make them permanent by adding them to ~/.bashrc (or ~/.zshrc), then source it.
  • Safety aliases like rm -i are conveniences, not guarantees — plain commands still work elsewhere.

Ten minutes of setup today saves you hours over a year — and building your own shortcut collection is one of the first steps where the shell starts feeling truly yours.

Top comments (0)