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)
|
0 1 2 3 4 5 6 |
alias shortname='full command here' |
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:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Quick detailed list (most popular alias ever) alias ll='ls -la --color=auto' # Even better – human-readable sizes alias ll='ls -lah --color=auto' # Git status shortcut (super common for developers) alias gs='git status' # Clear screen + list files alias cls='clear && ls' # Go home quickly alias home='cd ~' |
After typing any of these → press Enter. Now just type ll or gs → magic!
2. See all your current aliases
|
0 1 2 3 4 5 6 7 |
alias # shows everything alias ll # shows only one |
You will see something like:
|
0 1 2 3 4 5 6 7 |
alias ll='ls -lah --color=auto' alias gs='git status' |
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:
- Open the file:
|
0 1 2 3 4 5 6 |
nano ~/.bashrc # or vim, code, gedit, whatever you like |
- Scroll to the bottom (or find the place where other aliases are)
- Add your aliases, example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# My personal shortcuts alias ll='ls -lah --color=auto' alias la='ls -A --color=auto' # almost all, but not . and .. alias l='ls -CF --color=auto' alias gs='git status' alias gc='git commit' alias gp='git push' alias update='sudo apt update && sudo apt upgrade -y' # Ubuntu/Debian # or for Fedora: alias update='sudo dnf update -y' alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' |
- Save and exit (Ctrl+O → Enter → Ctrl+X in nano)
- Reload the file so changes take effect now:
|
0 1 2 3 4 5 6 7 8 |
source ~/.bashrc # or just . ~/.bashrc |
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:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
# Ask before remove / overwrite alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Make sudo forget – useful when alias has sudo inside alias sudo='sudo ' |
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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# Docker shortcuts alias dps='docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"' alias dcu='docker-compose up -d' alias dcd='docker-compose down' # Kubernetes quickies alias k='kubectl' alias kgp='kubectl get pods -o wide' alias kgn='kubectl get nodes' # Find big files fast alias big='du -ah . | sort -rh | head -n 20' # Weather (needs curl) alias weather='curl wttr.in/Hyderabad' # My IP alias myip='curl ifconfig.me' # Create dir + cd into it (power alias!) alias mkcd='f(){ mkdir -p "$1" && cd "$1"; }; f' # Use it: mkcd project/new/deep/folder |
Try mkcd test-folder/sub → it creates everything and jumps inside!
6. Delete / remove an alias
|
0 1 2 3 4 5 6 7 |
unalias ll # remove one unalias -a # remove ALL aliases (careful!) |
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
- Type:
|
0 1 2 3 4 5 6 7 |
alias ll='ls -lah --color=auto' ll |
- See nice colored list?
- Make it permanent:
|
0 1 2 3 4 5 6 7 |
echo "alias ll='ls -lah --color=auto'" >> ~/.bashrc source ~/.bashrc |
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! 😄
