Course outline · 0% complete

0/29 lessons0%

Course overview →

Making it yours: .bashrc and aliases

lesson 7-2 · ~8 min · 20/29

Your shell's startup file

Every variable and setting you make dies when the terminal closes. To make things permanent, bash reads a startup file every time a new interactive shell opens: ~/.bashrc (on some systems ~/.bash_profile).

Notice the name: it starts with a dot, so it's one of those hidden "dotfiles" you revealed with ls -a back in lesson 2-3. Anything you write in it (variables, PATH additions, aliases) is re-applied in every future terminal.

After editing it, either open a new terminal or reload it in place:

source ~/.bashrc

This is where the terminal stops being a generic tool and becomes your tool. Developers grow fond of their dotfiles — many keep them in version control so a new machine feels like home in minutes.

Aliases: your own shorthand

An alias gives a long command a short name:

alias ll='ls -l'
alias gs='git status'

Type ll and the shell silently substitutes ls -l before running. Aliases live in .bashrc so they exist in every terminal.

One technicality for the sandbox below: scripts don't expand aliases by default (they're an interactive-shell convenience), so the demo flips that on with shopt -s expand_aliases. In your real terminal, plain alias in .bashrc just works.

Code exercise · bash

Run it. We define an alias and then use it as if it were a real command.

Quiz

Where should `alias ll='ls -l'` live so it works in every terminal you ever open?

Code exercise · bash

Your turn. Enable alias expansion (first line is given), then create an alias `greet` that prints `hello from my alias`, and use it. Expected output: ``` hello from my alias ```

Problem

You just added an alias to ~/.bashrc and want it active in your CURRENT terminal without opening a new one. What command do you run?