Intro to the Command Line
Mini-workshop: Does command line interface (CLI), REPL, terminal, console, or shell sound scary? Don’t worry, we’ll learn about the basic computer interface that gives you super powers! If you want to work with Raspberry Pi, Linux, or many programming languages you need a basic familiarity with the CLI. This MILL-Mini will demystify the terminal so you can use it to get things done.
What is the Shell?
The command line is a text-based interface for efficiently getting stuff done. It’s often called the shell, terminal, console, cmd, or Bash, but ultimately the Shell is just a program on your computer like any other application. Its job is to act as a command shell, taking input from the user, ordering the computer’s operating system (OS) to execute the instructions, and returning the outputs.
Since the 1980’s the most common way to interact with a computer is via the graphical user interface (GUI), i.e. the windows, icons, and pointers of a desktop environment. Technically this is a shell. However, when people talk about the shell it usually refers to a command-line interface (CLI) made up of only text in a read-evaluate-print loop (REPL).
- you type something on the input line and press return
- the shell interprets the command
- the OS executes it
- the shell prints the output
Note that the shell does not execute commands itself. It calls on hundreds of other applications in the OS to get things done. It’s the most basic way to interface with your computer short of re-wiring it!
There are actually a lot of shells out there! Most OS come with one as a standard application. On Windows, CMD or PowerShell are normally available. These use a syntax and set of applications unique to Windows systems.
However, the most popular and commonly used is the Unix shell Bash (Bourne Again SHell) which is standard on Linux and Mac OS. Even if you are a Windows user, learning Bash will open up a powerful set of tools on your personal machine, in addition to familiarizing you with the standard remote interface used on almost all servers and super computers. If you want to control a Raspberry Pi or virtual machine in the cloud, you will need to use the Bash Shell.
On Linux or Mac, you already have Bash, just open a Terminal and you are ready to go. On Windows there are several options to add a UNIX-like style shell:
- Git Bash: installing Git for Windows adds the Git Bash shell. It comes with some of the standard UNIX applications (and you can add more). It is easy to install, integrates with Windows context menus (right click), and works well. (alternatively, use Git Bash with Cmder, which is fully portable, no install needed)
- Cygwin: a project that adds Unix style terminal to Windows including hundreds of utilities built and packaged to function natively on Windows. It includes more functionality than Git Bash and has a package system to install utilities (more info).
- MobaXterm: a non-opensource emulator similar to Cygwin with quite a bit of functionality and an optional paid version.
- Bash on Windows Subsystem: Windows 10 has the option to install a Ubuntu Linux subsystem which enables a native Bash shell with all the standard functionality. This is very handy, but installing takes a few steps.
The GUI window that hosts the text based CLI is often called a terminal emulator. Most people use the default that comes with your OS, but if you want to get fancy, there are a surprising number of options out there! Check out Guake (linux, dropdown), Hyper (slick looking, html/css based), ConEmu (windows, tabbed), upterm (linux, IDE terminal), Tilix (linux, tiling terminal), Cool Retro Term (linux, retro emulator with flickering CRT screen look).
Start up your terminal and type
This command asks the shell to “print working directory”.
The shell should give you some out put, like
That’s where we are right now!
/home/username is a file path representing our current location in the file system, just like navigating the folders in the GUI explorer.
Typically, on Unix systems the
/ is the root directory.
All other directories are organized below it.
Note: when using Git Bash on Windows,
cd ~should bring you to your Windows user directory, such as
C:\Users\username. However, the Windows alternatives locate the
/directories in a “fake” root directory, not the actual Windows file system root. For example, on Git Bash
cd /will bring you to something like
C:\. Cygwin and MobaXterm also place the “home” directory inside the fake root. For example, when using Cygwin,
/home/username, but will actually be located in the Windows
Take a look around:
-ais a flag, it adds an option to the command, in this case to show all files including hidden ones. There is often a short and long version,
ls --help(you can use
--helpwith almost any command! Linux also has a built in manual, try
cd Desktop(“change directory”)
cd ..(back one level)
- Save typing with
tabcomplete and up arrow for command history!!
- Copy & paste with your mouse or
ctrl+shift+V(short cuts may need to be enabled in your terminal’s settings options)
Work with files
Let’s make a test directory to play in:
cd ~(back to home directory)
mkdir test(“make directory”)
Now lets create a file in it:
echo hello world(terminal repeats what you said)
echo hello world > test.txt(the
>is redirect that prints the output to a file rather than the screen)
cat test.txt(“concatenate” displays one or more files in the terminal)
echo more text >> test.txt(the
>>redirects the output and appends it to the end of an file. A single
>would over write the file)
Play with it:
cp test.txt new/test.txt(“copy”)
mv test.txt new2/test2.txt(“move” changes location and/or name, be careful!)
rm -i test2.txt(“remove”, be careful there is no recycle bin!)
grep -i "hello" *.txt
Many programs run interactively inside the terminal (note: these don’t work well in Git Bash, but can be installed in Cygwin):
nano test3.txt(text editor in the terminal)
python(running another REPL system in the terminal)
curl http://www.lib.uidaho.edu/(utility to get URLs in terminal)
A good way to get familiar with the commandline is with some fun and games! These demos install amusing programs (note: demos based on Ubuntu. Only available on Linux or Bash on Windows Subsystem. However, most can be found in Cygwin packages.)
sudo apt-get install sl(apt-get is Debian / Ubuntu’s utility to find and install packages)
sudo apt-get install cowsay
cowsay "Hello world!"
sudo apt-get install fortune
fortune | cowsay(pipes
|send output to another command)
fortune | cowsay -f dragon
sudo apt-get install figlet
figlet Command Line!
figlet -f slant Command Line!(more font
-foptions: slant, shadow, small, smslant, smshadow, big, digital, bubble, script, smscript, lean, block, mini, banner)
sudo apt-get bsdgames
tetris-bsd(see full list)
Or watch Star Wars: