Log inSign up

Shortcuts and a well organised .bashrc

Type alias ll='ls -lah' to create a shortcut for the current session, then put the same line into ~/.bash_aliases (or a file under ~/.bashrc.d/) to keep it after logout. Reload with source ~/.bashrc. Keep aliases for short command substitutions, and use small shell functions when a shortcut needs arguments in the middle.

Topic: Bash scripting · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What is an alias and when is it the right tool?

An alias is a text replacement Bash applies to the first word of a command you type at the prompt. Typing ll after alias ll='ls -lah' runs ls -lah, and anything you type after ll is appended. That makes aliases ideal for long commands you type daily with a fixed beginning:

alias ll='ls -lah'
alias ports='sudo ss -tulpn'
alias jf='journalctl -f -u'
ll /var/www
jf nginx
alias            # list all current aliases
type ll          # shows what ll expands to

Aliases defined at the prompt vanish when you log out. They also work only in interactive shells: scripts ignore them unless shopt -s expand_aliases is set, and you should not rely on them in scripts anyway. In a script, write the full command or a function, covered in Functions in Bash.

Which startup file does Bash read, and when?

It depends on the kind of shell. A login shell, such as the one you get over SSH, reads /etc/profile and then the first of ~/.bash_profile, ~/.bash_login or ~/.profile it finds. An interactive non-login shell, such as a new tmux pane or typing bash, reads ~/.bashrc. The usual arrangement on every image makes the login file load ~/.bashrc, so in practice:

  • ~/.bashrc: aliases, functions, prompt, history settings, anything interactive.
  • ~/.profile (Ubuntu, Debian) or ~/.bash_profile (Rocky, Alma): environment variables and PATH changes that every login should have.
  • /etc/bash.bashrc (Debian family) or /etc/bashrc (RHEL family): system wide defaults; leave these alone and use /etc/profile.d/*.sh for global additions.
ls -la ~ | grep -E "\.(bash|profile)"
grep -n bashrc ~/.profile ~/.bash_profile 2>/dev/null

The second command confirms that your login file sources .bashrc. Non-interactive shells, such as cron jobs and scripts, read none of these files, which is why a script cannot see your aliases.

How do I make aliases permanent?

Keep them in their own file instead of the end of a long .bashrc. Ubuntu and Debian's default .bashrc already loads ~/.bash_aliases when it exists; Rocky and Alma load every file in ~/.bashrc.d/. A single snippet that works everywhere:

mkdir -p ~/.bashrc.d
grep -q "bashrc.d" ~/.bashrc || cat >> ~/.bashrc <<'EOF'
# load personal snippets
for rc in ~/.bashrc.d/*.sh; do
  [ -r "$rc" ] && . "$rc"
done
unset rc
EOF
cat > ~/.bashrc.d/aliases.sh <<'EOF'
alias ll='ls -lah'
alias ..='cd ..'
alias ports='sudo ss -tulpn'
alias df='df -h -x tmpfs -x devtmpfs'
alias update='sudo apt update && sudo apt upgrade'
EOF
source ~/.bashrc

The grep -q guard keeps the loader from being added twice when you run the snippet again. On Rocky and Alma replace the update alias with sudo dnf upgrade. Splitting snippets by topic (aliases, prompt, history) makes them easy to copy to a new server: just copy the folder.

When should I write a function instead of an alias?

Use a function whenever the shortcut needs its arguments somewhere other than the end, needs logic, or runs several commands. Aliases cannot place arguments:

cat >> ~/.bashrc.d/functions.sh <<'EOF'
mkcd() { mkdir -p -- "$1" && cd -- "$1"; }
logs() { sudo journalctl -u "$1" --since "${2:-1 hour ago}" --no-pager; }
bigfiles() { sudo du -ahx "${1:-.}" 2>/dev/null | sort -rh | head -n 15; }
EOF
source ~/.bashrc
mkcd ~/projects/demo
logs nginx "30 min ago"

Functions in .bashrc behave like commands in your interactive shell and accept defaults, quoting and conditions. bigfiles pairs well with the disk usage guide. If an alias and a function share a name, the alias wins at the prompt; unalias name removes the alias.

How do I get more useful command history?

Raise the history size, keep timestamps, avoid duplicates and append rather than overwrite when several sessions close. Put this in ~/.bashrc.d/history.sh:

HISTSIZE=50000
HISTFILESIZE=100000
HISTCONTROL=ignoreboth:erasedups
HISTTIMEFORMAT="%F %T  "
HISTIGNORE="ls:ll:pwd:exit:clear"
shopt -s histappend cmdhist

ignoreboth skips duplicate lines and lines starting with a space, which gives you a handy way to keep a command with a secret out of the history: type a space in front of it. histappend stops parallel SSH sessions from overwriting each other's history. Search history interactively with Ctrl+R, and list it with timestamps using history | tail.

How do I set a clear prompt that shows which server I am on?

Set PS1 in a snippet. A prompt that shows user, host and folder, and turns red for root, prevents running commands on the wrong machine:

cat > ~/.bashrc.d/prompt.sh <<'EOF'
if [ "$EUID" -eq 0 ]; then c=31; else c=32; fi
PS1="\[\e[1;${c}m\]\u@\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\\$ "
unset c
EOF
source ~/.bashrc

\u is the user, \h the short host name and \w the folder; the doubled backslash before the dollar sign survives the double quotes, so Bash shows # for root and $ for everyone else. The \[ and \] pairs wrap colour codes so Bash calculates line length correctly; without them long commands wrap onto themselves. Giving every server a meaningful host name helps here, see Change the hostname.

Which aliases are worth adding for safety?

A few interactive safety nets catch costly slips without getting in the way:

alias rm='rm -I'
alias cp='cp -i'
alias mv='mv -i'
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'

rm -I asks once before deleting more than three files or recursing, which is less annoying than -i for every file. Rocky and Alma already ship similar aliases for root. Do not let these become a habit you depend on: on a fresh server they are absent, so the care has to come from you. To bypass an alias once, prefix the command with a backslash, as in \rm file, or call command rm file.

How do I check and reload my changes?

Test syntax before you log out, since a broken .bashrc makes every new session print errors. Then reload the file in the current shell:

bash -n ~/.bashrc && for f in ~/.bashrc.d/*.sh; do bash -n "$f" || echo "error in $f"; done
source ~/.bashrc
bash -lic "type ll" 2>&1 | head -n 2

The last command starts a fresh interactive login shell and shows whether ll is defined there, which proves the files load at login. If a mistake locks you out of a usable shell, connect again with ssh -t deploy@198.51.100.24 bash --norc to get a shell that skips .bashrc, then fix the file. Keep your dotfiles in a Git repository so you can set up a new server quickly.

Questions about Aliases and a tidy .bashrc

Why does my alias not work in a script or cron job?

Scripts run as non-interactive shells, which do not expand aliases or read .bashrc. Use the full command or define a function in the script.

Do aliases work with sudo?

Only the first word is checked for aliases, so sudo ll fails. Adding alias sudo='sudo ' (with a trailing space) makes Bash check the next word too.

How do I share aliases with every user on the server?

Put a file into /etc/profile.d/, for example /etc/profile.d/team-aliases.sh. It is loaded by login shells for all users.

How do I remove an alias?

Run unalias name for the current session and delete the line from your snippet file so it does not come back at the next login.

Should I use .bash_profile or .profile?

Use whichever already exists on your image. If ~/.bash_profile exists, Bash ignores ~/.profile, so never create both by accident.