The Command Line

Using the command line is essential to computing proficiency.

slides | md

Contents

  1. Introduction
  2. About the command line 5 min
  3. Navigating the filesystem 5 min
  4. Creating and editing files 5 min
  5. Running programs 5 min
  6. Discussion 5 min
  7. Keep practicing
  8. Assessment
  9. References

Introduction

Review the following sections and perform the activities on your own or with your group.

Perform the task(s) when you see this 👉 emoji

Learning Objectives

Students who complete the following will be able to:

  • Explain what the command line is and common uses
  • Open a command line program on their computer and execute basic commands
  • Navigate, create, and edit files on their computer using the command line
  • Edit and run a Python script using the command line
  • Use a web reference to look up shell commands

About the command line

The command line is a text interface for your computer. You type commands, and the computer operating system (OS) will run them when you press return.

About the command line

The command line not that different from using a GUI (Graphical User Interface) with a mouse. It allows you to create and edit documents, or manage files and folders on your computer, just as you would with Windows Explorer or the MacOS Finder.

In fact, when you use the GUI, your clicks and interactions with the "pictures under glass" on screen are just sending the same commands to the operating system.

Let's see what the command line can do that your mouse cannot...

Term Description
OS Operating System
CLI Command Line Interface
Shell The application where you use the command line
Bash ("Bourne Again Shell") A type of shell
Zsh ("Z Shell") The new default shell used in macOS Catalina and later
Shell programming Programming and running scripts (e.g. .sh) using a shell

👉 Installation

Running commands

Open your command line app.

You'll see the command prompt, signified by the $ % or # symbol, where you will type each command and press return to make the computer execute it.

whoami

whoami

👉 Type this command and press return. If you make a mistake, press Ctl + C to cancel.

whoami

The whoami command will return or output your username.

pwd

Use pwd (Print Working Directory) to find out where you are in the filesystem.

Parameters

When you type on the command line, the shell that your terminal uses (e.g. bash, zsh, etc.) can also accept parameters that change what the command will do. These are called "flags".

👉 For example, the id command (like whoami) will return login information, but using the following flags will be more specific.

id -un

👉 The man command shows the manual file associated with a command, including all flags. Add the command name at the end.

man id

which

👉 You can also combine commands. For example, which returns the file path of other programs on your system.

which whoami
# -> /usr/bin/whoami

The hash symbol # is used for comments in bash scripting, and so is skipped by the shell when evaluating the input. These are displayed to show what the output will be.

👉 Special operators can create new commands from many. The double ampersand && runs all of these.

whoami && pwd && echo Hello world!
# -> <outputs all three in series>
# -> owenmundy
# -> /Users/owenmundy
# -> Hello world!

👉 Let's learn how to move around. First, confirm your location:

pwd

Most commands are abbreviations for the English equivalent of what they perform.

👉 Run these commands and try to guess what they do.

ls
ls -a

ls

If you guessed "list" and "list, show all" you were correct!

ls lists the contents of your present "working directory", and the -a flag displays all files.

The a displays all files, including those beginning with a ., which are hidden configuration files for software.

Adding an l ("long form") flag displays the contents' file sizes and last edit dates.

👉 Now, cd ("Change to a new Directory") and list the files. Where you are?

cd /
ls -la

Using cd / changes our current working directory to the root of the computer, where you'll find files your OS uses.

👉 Return to your home directory (the default location for a new shell) with (a "tilda" ~ + /)

cd ~/

Creating and editing files

Let's make a new folder in our home directory to create some test files.

👉 First, make sure you have a Finder or Explorer window open so you can see the effects of your work in the GUI.

cd ~/ # confirm you are home
mkdir test # make a new directory called "test"
cd test # change to the directory
pwd # confirm you are in <username>/test

Create a new file

👉 Use touch to create a new file named "hello.txt":

touch hello.txt

On Windows, Git Bash defaults to the Vim editor. On Mac you can also use Nano.

👉 Either editor can open (or if it doesn't yet exist, create and open) a file we created above hello.txt

vim hello.txt

Vim has two modes: command and insert. In the command mode, you can move around the file, delete text, save, etc. In the insert mode, you can insert text.

👉 To change to the insert mode, press i. Type some text.

vim insert

👉 Change back to command mode by pressing ESC, and save your file by pressing :wq (:wq) (write and quit). Once you do this, run the following to confirm the file exists and see its contents.

ls -la
cat hello.txt
# -> hello world!

vim insert

Running programs

Excellent! Now, in our final section, we'll create and run a Python script with Vim.

👉 Confirm you are still in <username>/test, and "clear" the terminal (you can still scroll up to see previous commands)

pwd
clear

👉 Create and open a plain text file with the .py extension.

vim script.py

Add Python code

👉 In the Vim editor, enter insert mode using i, and paste or type the following:

string1 = "I"
string2 = " <3"
string3 = " the CLI"
joined_string = string1 + string2 + string3
print(joined_string)

👉 Press ESC and then :wq to save and quit the editor.

Run Python code

To run your file, find out which version of python you have installed by running either of these.

which python
which python3

And use that version with the name of the file:

python script.py

Congratulations! 🎉   You've created a file that runs its own script!

⚠️ Windows 11 users may need to install Python and use PowerShell for this step.

Discussion

Discuss the following with your group and share a short demo on the above with the class.

  1. Demonstrate example commands that show
    1. how to navigate the filesystem
    2. how to create files
    3. how to run a program
  2. Of the commands you used, which reminded you of concepts you already know about computers?
  3. Revisit the learning objectives for this section. Did you accomplish the goals in this lesson? What questions do you have?

Keep practicing

Continue learning the command line:

Assessment

Ready to test your skills? See if you can

  1. Try out any commands in these cheatsheets.
  2. Write a simple bash script
  3. Take a quiz

Quiz

  1. How can you autocomplete commands once you start typing?

References

Presentation comments ...