Exercise: Your Toolkit, Version 1

Build the first version of your own toolkit, a file of aliases, two small functions and a prompt in your colors, answer four questions about startup files and installing tools, and let a check script try every shortcut for real.

Apprentice · Unix · Linux · Updated

Every experienced terminal user has a toolkit: a file of short names and small helpers they've collected over the years and take to every computer they use. Today you start yours. Version 1 holds a better ll, a shortcut up one folder, an rm that asks first, two small functions, mkcd and bak, and a prompt in colors you choose. A check script loads your toolkit into a fresh shell, tries every shortcut for real, and tells you which ones work.

Before you start

You need a terminal: the Terminal app on a Mac, or Ubuntu on Linux or in WSL on Windows. The check runs with bats, a small program that runs checks written for bash. If bats --version doesn't print a version number yet, install it:

sudo apt install bats       # Ubuntu, and Ubuntu in WSL
brew install bats-core      # a Mac, with Homebrew

sudo asks for your password, the one you use to sign in; nothing shows on the screen while you type it.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-bash-your-toolkit-v1, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your terminal-practice folder. Or clone it with Git.

The folder holds:

  • toolkit/bash_aliases: your toolkit, with one example done and six jobs to do, each a comment saying what to add.
  • answers.txt: four questions about startup files and installing tools, each with a numbered line for your answer.
  • check.bats: the check. You don't need to change it.

What to do

Open a terminal and cd into the exercise folder, the one with answers.txt in it. Open the toolkit with nano toolkit/bash_aliases. Each job is a comment, with room under it for your lines:

# Example: c clears the screen.
alias c='clear'

# 1. ll: a long list of a folder, hidden files included, with sizes such as 2.0K.
  1. ll: a long list of a folder, hidden files included, with sizes such as 2.0K (Aliases and Small Functions).
  2. ..: go up one folder.
  3. rm: ask before deleting each file, the safer habit from Deleting Safely: rm and rmdir.
  4. mkcd: a function that makes a folder, and any folders on the way to it, then goes into it. mkcd "trip photos/day 1" must work, spaces and all.
  5. bak: a function that copies a file to the same name with .bak on the end, so bak notes.txt makes notes.txt.bak. It's the backup habit from Startup Files: .bashrc, .zshrc and source, in three letters.
  6. Your prompt: a PS1 with the folder you're in, at least one color, the color set back to normal, and \$ at the end (A Prompt in Your Colors). The colors are your choice.

Save, then load the toolkit into the shell you're in and try each shortcut as you go: source toolkit/bash_aliases, then ll, then mkcd test-folder. Finally, answer the four questions in answers.txt, using Startup Files: .bashrc, .zshrc and source and Installing Tools: Homebrew and apt.

Check your work

From the exercise folder:

bats check.bats

At first most checks fail. That's expected: each job you finish turns its check into a ✓. A failing check names the job and gives a hint, for example There's no bak function in your toolkit yet. One line between { and } is enough: cp, the file, then the same name with .bak on the end. The check works in a throwaway folder of its own, with a fresh bash that has loaded only your toolkit: it never reads or changes your own ~/.bashrc.

Hints

  • An alias error on loading? alias name = 'command', with spaces around the =, is the usual cause. The message names the line.
  • Spaces in a name stay in one piece only when $1 is in double quotes: "$1". That goes for mkcd and for bak.
  • A function needs its braces: name() { on the first line and } on the last.
  • Colors: each code goes inside \[ and \], like \[\e[1;36m\], and \[\e[0m\] sets the color back to normal. Try your prompt at the prompt first, then copy the line that you like into the toolkit.
  • On a Mac, your Terminal runs zsh. The aliases and functions work there too, but the PS1 line is bash's; the check runs it in bash.

When you're done

Every check passes:

check.bats
 ✓ the toolkit file is here
 ✓ the toolkit loads without errors
 ✓ 1) ll lists everything, with sizes people can read
 ✓ 2) .. goes up one folder
 ✓ 3) rm asks before deleting
 ✓ 4) mkcd makes a folder and goes into it
 ✓ 5) bak keeps a copy with .bak on the end
 ✓ 6) the prompt shows the folder, in color
 ✓ answer 1) the file bash reads when a terminal opens
 ✓ answer 2) the command that loads the toolkit into this shell
 ✓ answer 3) checking whether jq is installed
 ✓ answer 4) one command that installs all four tools

12 tests, 0 failures

Now make it yours for real. Copy the toolkit to ~/dotfiles/bash_aliases and link it into place, as in Your Dotfiles: A Setup You Can Carry, and open a new terminal: your prompt and your shortcuts are there, and they will be in every terminal from now on. What would you add for version 2?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-bash-your-toolkit-v1), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (Ubuntu in WSL on Windows), send your work to your new repo. If you downloaded the ZIP:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-bash-your-toolkit-v1.git
    git push -u origin main
    

    If you cloned the starter instead, point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-bash-your-toolkit-v1.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-bash-your-toolkit-v1, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

A toolkit file is how working developers and administrators make any terminal feel like their own, and yours now has a first version you wrote, tested and can carry anywhere.

Key terms

Related