đź”— Coming Online: Introduction to the Command Line and Version Control
Weekly Assessment Goals
We will review concepts on Tuesday and all material should be submitted before next Thursday’s class.
- Introduction/review of the command line (with the instructor, optional)
- Complete command line assignment
What is the Command Line and how do we work with it?
- Command Line might sounds a bit intimidating
- But ask yourself, how do you find files and manipulate files on your computer? Probably using Windows Explorer or Mac Finder, right?
- Command Line is a text-based way of doing the same thing you do with your files
- Command Line is used a lot in coding because we manipulate our code files and run them all in the terminal
This is a helpful but brief overview. Often you might get confused, because we’ll talk about command line interface/CLI, terminal, shell, console. While these terms are overlapping, my colleague Shane Lin at UVA helped me understand that a lot of the differences between these terms has to do with the history of early computers, when computers looked like this 👇🏽.
According to Shane,
Operators would interact with these machines through a smaller device dedicated to input and output, called a console or terminal. In the very early days, these consoles would use literal printers to print out output from the computer onto paper. In modern computing, “console” and “terminal” are analogies for software programs that replicate these technologies. The application “Terminal” on MacOS is a terminal software.
A “shell” is a program that lives within a terminal that interprets commands from users and parses output from the computer. The name Shell comes out of the influential Unix operating system and is intended to convey the idea that shell programs completely surround and hide away the complex details of different underlying computer systems from users to provide a simplified and common interface. BASH (Bourne Again SHell) and Zsh (Z Shell) are the most popular shell programs.
We’ve already interacted with the command line, terminal, and shell through our installation instructions.
Let’s do an exercise together
-
Open the cheatsheet. Copying is encouraged in coding, so make sure you use the cheatsheet!
-
Create a directory called
workspace
in your home directory.
mkdir ~/workspace
-
Create the following directory structure in your
workspace
directory, usingmkdir
.workspace +-- cli +-- practice +-- create
- Enter (change) into the
create
directory, usingcd
. Remember to use tab completion. -
While in this directory, create a new file named
is310
.touch is310
-
Put some simple content in the file using the
echo
command.echo 'Coding is ...' > is310
-
Now use the
cat
command to read those contents.cat is310
- Remove the
is310
file you created earlier with therm
command. cd
back up to thecli
directory.- Remove the
practice
directory and all of its contents.
Let’s dive further through the following tutorials developed by Melanie Walsh:
Command Line Assignment
Time to get lost in the Command Line
In Class Assignment:
- First, using the command line commands and your terminal create a series of nested directories and files.
- At least one your directories needs to have nothing in it.
- At least one of your directories needs to have a file with the text, “You’re lost” on it.
- At least one your directories, needs to have a file with the text “You’ve made it to the center of the maze!”
- Once you’ve completed your corn maze, pair up with another student and have them try and navigate to the center of your maze using command line commands.
- Since we’re virtual, feel free to share your via
discordgoogle drive folder with your classmates.
- Since we’re virtual, feel free to share your via
Additional/Homework Assignment: Following Melanie Walsh’s working with Text Files in the command line tutorial, complete the following steps:
- Select a book from Project Gutenberg and download a text file version using the command line (you can use the wget command or curl command).
- Using the command line, rename your downloaded file to be the title of the book.
- Count the number of words in your book file and print the result to the screen.
- Search for a word (would recommend not using “the”) in your book file and print the number of times it appears.
- Take a 📸 screenshot of your output and send it to the instructor in discord.
More Information
In Depth Dive into the Command Line and Shells
At its base, a shell is simply a macro processor that executes commands. The term macro processor means functionality where text and symbols are expanded to create larger expressions.
A bash shell is both a command interpreter and a programming language. As a command interpreter, the shell provides the user interface to the rich set of utilities for your operating system. The programming language features allow these utilities to be combined. Files containing commands can be created, and become commands themselves. These new commands have the same status as system commands in directories such as /bin, allowing users or groups to establish custom environments to automate their common tasks.
Shells may be used interactively or non-interactively. In interactive mode, they accept input typed from the keyboard. When executing non-interactively, shells execute commands read from a file.
Shells also provide a small set of built-in commands (builtins) implementing functionality impossible or inconvenient to obtain via separate utilities. For example, cd, break, continue, and exec cannot be implemented outside of the shell because they directly manipulate the shell itself. The history, getopts, kill, or pwd builtins, among others, could be implemented in separate utilities, but they are more convenient to use as builtin commands. All of the shell builtins are described in subsequent sections.
While executing commands is essential, most of the power (and complexity) of shells is due to their embedded programming languages. Like any high-level language, the shell provides variables, flow control constructs, quoting, and functions.
Shells offer features geared specifically for interactive use rather than to augment the programming language. These interactive features include job control, command line editing, command history and aliases.
Resources
- Ian Milligan and James Baker, “Introduction to the Bash Command Line,” The Programming Historian 3 (2014), [https://programminghistorian.org/en/lessons/intro-to-bash]. This is an introduction to the Bash shell, which will serve well enough as an introduction to other shells like Zsh as well.
-
[Bash Basics Part 1 of 8 Access and Navigation](https://youtu.be/eH8Z9zeywq0?t=885) - Beginner’s Guide to the Bash Terminal
- The Most Important Thing You’ll Learn in the Command Line
- Go through the CodeAcademy command line course.
- Shell Scripting Tutorial