Read

Module 10 · Putting it all together

Aliases and small shell shortcuts

Shorten the commands you type every day with aliases and tiny functions, and make them survive a new login by saving them in ~/.profile.

What you will learn

  • Define, list and remove aliases with `alias` and `unalias`.
  • Know when an alias is not enough and a shell function is needed.
  • Make aliases and functions permanent through `/root/.profile`.

An alias is a nickname the shell expands before running a command. If you type ls -l fifty times a day, alias ll='ls -l' lets you type ll instead. The shell replaces the first word of the line with the alias text and then reads the rest normally, so ll /etc becomes ls -l /etc. Aliases are a feature of the interactive shell only; they are not programs and no other process can see them.

~% alias ll='ls -l'
~% alias la='ls -a'
~% alias
ll='ls -l'
la='ls -a'
~% ll /etc/hostname
-rw-r--r--    1 root     root             6 Jun  3  2018 /etc/hostname
~% type ll
ll is an alias for ls -l
~% unalias la
~% la
sh: la: not found

Use single quotes around the alias text so that nothing in it is expanded at definition time, and no spaces around =, exactly like a variable. alias with no arguments lists every alias, alias ll shows one, unalias ll removes it and unalias -a removes all of them. type NAME is the quickest way to find out what a word really is: an alias, a shell builtin, a function or a file on the PATH.

Bypassing an alias

A common safety alias is alias rm='rm -i', which asks before each deletion. When you want the plain command for once, put a backslash in front, \rm file, or write command rm file. Both stop the alias expansion. Calling the program by its full path, /bin/rm, works too.

When an alias is not enough: functions

An alias can only add text at the start of the line. It cannot take an argument and put it in the middle, or run two commands on the same argument. For that, write a small function (lesson 87). The classic one creates a directory and enters it in one go:

~% mkcd() { mkdir -p "$1" && cd "$1"; }
~% mkcd lab/l92/new/project
project% pwd
/root/lab/l92/new/project
project% type mkcd
mkcd is a shell function

A function runs in your current shell, so its cd sticks, something a script could never do (a script runs in a child process). Keep functions for anything with arguments and aliases for plain abbreviations.

Making them permanent

Aliases and functions live in the memory of one shell and vanish when it exits. To have them every time, write them in a startup file. As lesson 70 explained, a login shell reads /etc/profile and then ~/.profile; the console shell of this VM is a login shell (its name in ps is -/bin/sh, the dash means login), so /root/.profile is the place. The file may not exist yet; >> creates it. To load it into the shell you already have, source it with a dot.

~% echo "alias ll='ls -l'" >> /root/.profile
~% echo 'mkcd() { mkdir -p "$1" && cd "$1"; }' >> /root/.profile
~% cat /root/.profile
alias ll='ls -l'
mkcd() { mkdir -p "$1" && cd "$1"; }
~% . /root/.profile

Commands in this lesson

CommandWhat it does
alias ll='ls -l'Define an alias.
aliasList all aliases.
unalias llRemove an alias (`-a`: all of them).
type nameAlias, builtin, function or file?
\lsRun the command ignoring any alias.
name() { …; }Define a shell function (takes arguments).
. /root/.profileReload the startup file in this shell.

Quiz

  1. After `alias ll='ls -l'`, what does `ll /tmp` run?

    • `ls -l /tmp`
    • `ll /tmp` as a program from the PATH
    • `ls /tmp -l ll`
  2. You defined an alias, opened a new login and it is gone. Why?

    • Aliases expire after an hour.
    • It only lived in the old shell's memory; save it in `/root/.profile`.
    • Root cannot have aliases.
  3. `rm` is aliased to `rm -i`. Which runs the plain `rm` once?

    • `rm -f`
    • `\rm file`
    • `alias rm`
  4. You want `mkcd DIR` to create DIR and enter it. Alias or function?

    • An alias: `alias mkcd='mkdir -p && cd'`
    • A function, because the argument is needed twice
    • A script in /root/bin
  5. What does `type ll` tell you?

    • Whether `ll` is an alias, builtin, function or a file, and what it expands to
    • The file type of `ll`
    • How many times you used `ll`

Practice

  1. Make the alias `lt` (meaning `ls -lt`, newest files first) permanent by adding it to `/root/.profile`.

  2. Add a function `mkcd` to `/root/.profile` that creates the directory given as its argument (with parents) and enters it. After `. /root/.profile`, `mkcd /root/lab/l92/a/b` must leave you in that new directory.

  3. Suppose `ls` is aliased to something odd. Type a command that lists `/etc` with the real `ls`, bypassing any alias.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.