Chapter 17: Bash Alias

What is a Bash alias? (super simple first)

An alias is a shortcut name you give to a longer command (or chain of commands). Instead of typing something long and complicated every time, you type a short word → Bash replaces it with the real command behind the scenes.

Example:

  • You always type ls -la –color=auto to see detailed files with colors.
  • You get tired of typing it → so you make an alias called ll.
  • Now just type ll → Bash secretly runs ls -la –color=auto.

It’s like giving nicknames to your friends so you don’t say full names every time.

1. How to create an alias (basic syntax)

Bash

Important rules:

  • No spaces around the =
  • Put the whole command in single quotes ‘ ‘ (or double quotes ” ” if you need variable expansion)
  • Alias name cannot have spaces or special characters (except underscore _)

Real examples to try right now:

Bash

After typing any of these → press Enter. Now just type ll or gs → magic!

2. See all your current aliases

Bash

You will see something like:

text

3. Make aliases permanent (so they survive reboot / new terminal)

Aliases you type in the current terminal die when you close it.

To make them live forever → put them in your ~/.bashrc file (or ~/.bash_aliases if your distro uses it).

Steps:

  1. Open the file:
Bash
  1. Scroll to the bottom (or find the place where other aliases are)
  2. Add your aliases, example:
Bash
  1. Save and exit (Ctrl+O → Enter → Ctrl+X in nano)
  2. Reload the file so changes take effect now:
Bash

Now open a new terminal → your aliases are there forever!

4. Safety aliases (very smart – prevent accidents)

Many people add these to protect from dangerous commands:

Bash

The trailing space in sudo=’sudo ‘ is magic → it allows the next command to be expanded too (e.g. sudo ll will become sudo ls -lah …)

5. Advanced / fun / real-world examples (copy-paste ready)

Bash

Try mkcd test-folder/sub → it creates everything and jumps inside!

6. Delete / remove an alias

Bash

7. Quick cheat-sheet table

What you want Example alias line When useful
Detailed file list alias ll=’ls -lah –color=auto’ Daily use
Git status alias gs=’git status’ Every git user
Safe remove/copy/move alias rm=’rm -i’ Prevent oops moments
Update system alias update=’sudo apt update && sudo apt upgrade’ Ubuntu/Debian users
Multiple .. shortcuts alias ..=’cd ..’ alias …=’cd ../..’ Fast navigation
Create dir & enter alias mkcd=’f(){ mkdir -p “$1” && cd “$1”; }; f’ Super productive
Docker ps nice alias dps=’docker ps –format “table …”‘ Container work
Show weather alias weather=’curl wttr.in/Hyderabad’ Quick check

8. Best practices (what experienced people do)

  • Use short but clear names (ll, gs, dps – not super-long)
  • Don’t override dangerous commands without thinking (avoid alias ls=’rm -rf /’)
  • Put personal aliases at the bottom of ~/.bashrc
  • Group them with comments (# My git aliases)
  • Test first in current terminal before saving
  • If alias doesn’t work → check if real command exists (type ll)

9. Quick practice right now

  1. Type:
Bash
  1. See nice colored list?
  2. Make it permanent:
Bash

Done!

Now tell me:

  • What commands do you type a lot? (git? docker? apt? cd long paths?)
  • Want me to help make 5 custom aliases just for you?

Or ask anything – “how to make alias with arguments?” or “why my alias not working?” – we’ll fix it together! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *