Skip to content

Your own shortcuts: alias and ~/.bashrc

IntermediateLesson 25 of 666 min

Make a short name for a long command with alias, keep it in ~/.bashrc so it survives a new terminal, and load it with source. Practise in your browser.

Some commands you type every day, and some are long. An alias gives one a short name:

alias ll='ls -alF'
ll

Anything you type after an alias goes on the end, so ll projects runs ls -alF projects. Use single quotes around the command and, as with variables, no spaces around the =.

  • alias on its own lists your aliases; type ll says what a name runs.
  • unalias ll removes one.
  • An alias can hold several commands: alias up='cd .. && ls'.

Making it last

Aliases disappear when the terminal closes. To keep one, put the alias line in ~/.bashrc, a file bash reads every time a terminal opens. It's where your settings live:

cat ~/.bashrc

You can add a line with an editor, or with echo and >>:

echo "alias gs='git status'" >> ~/.bashrc

Careful:Two arrows, >>, add to the end. One arrow, >, replaces the whole file, and everything else in your .bashrc is gone.

A change to .bashrc takes effect in new terminals. To load it into this one now, run source ~/.bashrc.

Your turn

Make an alias called projects that runs cd ~/projects && ls, and try it. Then make it last: add the same alias line to the end of ~/.bashrc, keeping what is already there, and load the file with source.

Next