Bash: in 5 Minutes or Less
Housekeeping Items 🏡
In this quick tutorial, we’ll be using the
bash shell, but hey — use what you want. My personal favourite is
zsh, which can be downloaded using
homebrew — more on that tool another time. A shell is basically an interpreter for direct requests to the OS, using a command line rather than a graphical user interface. A “terminal” is a way to access this shell. We’ll be using “<>” to denote when something is a filler. For example, if you see
<file name>.txt, that means any text file you’ll use in the example. For most commands there are many options and flags which we won’t cover. Oh, and we’ll use
$ as a cursor at the start of commands — you don’t actually type this!
Getting Comfortable 🚶
When diving into new technology, it is a rite of passage to print the famous Hello, world statement. Once you have your terminal open, try the following command:
echo Hello, World!. Just like other environments, the shell can handle variables as well. Try setting a variable, and using
echo in order to print it. Reference the variable by prepending a
$ to the
< variable name>.
$ greeting="Hello, World!"
$ echo $greeting
Creating & Navigating Directories 📁
Directories are the command-line verbiage we use when describing what the GUI would consider a “folder”. It’s a place to store things — like files. Directories can be nested in a parent/child tree-structure. Use the following to check out your current directory:
pwd. Then, use the
cd followed by a tilde (a fancy word for this squiggly line
~) to navigate to your home directory. As you’ll see by the results,
pwd is a command which means “print working directory”. “cd” means “change directory” — simple, right? Use
ls to list the contents of a directory. Can you guess what
mkdir followed by a directory name does? Try the following commands further below. When you’re done playing around, you can use
$ mkdir code
$ cd code
Tips: Use the logical AND operator
&& between expressions to fit multiple commands on one line. For example, we can create that “code” directory, navigate into it, and confirm by using
mkdir code && cd code && pwd. You could also use
;, but this can be more dangerous. Also,
|| works as a logical OR operator to execute a command only if the first doesn’t succeed. You can also move up and down the directories in the filesystem using
cd with options like
.. (parent directory) and
— (previous directory).
Working with Files 📃
You now know a bit about directories. Directories can hold files. To create a file, you’ll use
touch followed by the filename (because this is super easy). You can open the newly created file by simply using
open <filename>.<extension>. Bash also has a built-in text editor if you want to do everything in the terminal. Try
vim <file name>. Warning:
vim can seem finicky, so remember these shortcuts: type
i to start editing, and when you’re done — use the
ESC key to finish up. You can save by then typing
:w and quit by
:wq to save AND quit. Moving files can also be tricky, as UNIX based systems use the
mv command for both moving locations and renaming. Look at the difference between the commands below:
mv chocolate.txt vanilla.txtwill rename a text file from chocolate to vanilla.
mv chocolate.txt dessert_flavours will move the chocolate text file into a directory for dessert flavours.
You can move multiple files at a time by simply listing them followed by a target directory where you wish to move them. Removing a file is as simple as using
rm <file name>.
Searching, Sorting, and Viewing 🔍
It seems you’ve gotten the hang of working with files and directories. To help you with managing all of these items — we’ll walk through how to find, sort, and preview them.
You can use
find in a few different ways: if you follow it with a directory name, it will list everything inside of it. You can also search for specific files across directories like
find /<directory> -name <file name>.<extension>. You can also use
grep with similar options — which will process all text and return any matching results. But what if you don’t know the name? You can use wildcards (
To view the content of files, try using
cat to show from the start — or
tac from the end. Use
tail similarly to preview the top or bottom portion of a file. You can also use
less to view the file inline, with interactive scrolling.
Finally, let’s play with sorting. We’ll create a new directory, a text file, and add a list of something that you want to sort. This should be a breeze given what you learned above. (Hint: make the list in random order or else it isn’t very fun).
Using a simple
sort command, it will display the contents of the file sorted in alphabetical order. There are a number of options available for sorting in different ways.
Connecting Commands with Plumbing 🔧
You may notice in the last example above, that if you
cat the file again — it isn’t sorted. That’s because the sort command is only processing it and displaying the results — not actually modifying the content. But what if you wanted to
sort and create a new file?
Enter, redirects, pipes, and filters. The pipe (
|) operator is used to feed the output of one command into the following command as input, and a redirect (
>) can be used to direct output into a file. I can see the wheels turning in your head.
sort citylist.txt > sortedcitylist.txt; head sortedcitylist. Cool right? Try playing around with pipes to combine different commands and inputs/outputs. What does the following do?
$ sort citylist.txt > citylistsorted.txt
Running Scripts to Automate Workflows 👟
As you likely realize, doing all this manually from the command line is tedious. The purpose of the shell is to do things efficiently and programmatically. We can piece together commands like the ones used in the examples into a script — a
.sh file that can be run by the shell. The script starts with a shebang: in this case
#!/bin/bash and can contain variables, commands, and conditional logic. For example, we can use some commands like these in a
#!/bin/bashecho "Hello, $USER. Add a city to the list:";
read newcity;echo "$newcity" >> citylist.txt;sort citylist.txt > citylistsorted.txt;
echo "City has been added to list.";
To make a script executable, you’ll use
chmod -x city.sh. Then, run it using
sh city.sh command. Any ideas what we’ll see when we
cat the sorted city list file after running this program?
Additional Resources 📚
Well, this has been a fun first look (or refresher) into the world of shell, bash, and unix/linux based operating systems. There is so much more you can do — so keep playing around!