Linux Command Line

Intro to bash

Bash is a shell, which is a program that lets you interact with your computer and run other programs. A shell is the main interface between you and a Linux (or other Unix-like) computer. Every computer programmer needs to use a shell from time to time, and bash is the default shell on most Linux computers.

Command line interfaces vs. graphical interfaces

Bash provides a command line interface, which is a text-based interface to your computer. The shell prompts you to type commands (usually one at a time) and then runs the commands you type. Any output or errors from your command are printed to the screen.

Most modern computers provide a graphical shell that looks like an "electronic desktop" with windows you can open, move, and resize with your mouse. You are probably familiar with the Windows Shell, or with macOS' Finder, Dash, and Mission Control. You can also interact with these computers using a text-based shell; Windows has CMD.exe, while macOS provides a shell through Terminal.

Why have both kinds of shell? Graphical shells are easy to learn for everyday use, but they make some things slower or harder. A command line interface is more intimidating than a point and click interface, but it provides powerful and flexible tools such as automating tasks or processing large groups of files in a single command.

Text-based shells are also frequently used to remotely access another computers. The remote computer might be physically far away; it might lack a keyboard or monitor of its own; or might be a virtual machine running inside another computer! A command line interface on a remote computer is easier to manage than a graphical one - if the remote can even handle graphics at all!

Shells vs. terminal emulators

Programmers interacted with early computers through a terminal, an electronic keyboard and screen. Their shell read input from the terminal keyboard, then printed output on the terminal screen (the earlier-still teletype had no screen and printed output on paper like an electronic typewriter!).

Modern computers provide a command line interface with a terminal emulator, which appears as a text-based interface in a desktop window. Windows provides both CMD and PowerShell (or the newer Windows Terminal), while macOS has the Terminal app. There are many terminal emulators (each with their own special features), but all provide a similar experience.

Shells are regular programs, not part of Linux. This makes it easy to replace one shell with another (or write your own!). Most shells have the same basic commands, but can have different advanced features.

Bash (the "Bourne-Again shell") was released in 1989 as a free software version of the Bourne shell (which upgraded the Thompson shell written by Unix creator Ken Thompson). Other shells include the Korn shell (ksh), C Shell (csh) and Z shell (zsh).

Bash was the default shell on macOS Mojave (10.14) and earlier; on Catalina (10..15) the default shell is zsh.

Catalina users who want to make bash their default shell can use:

$ chsh -s /bin/bash

You'll need to restart Terminal for the change to take effect.

Bash shell prompt

Each time you begin a shell session (say, by opening a terminal emulator) you'll see a shell prompt (also called a command line) waiting for your input.

Examples of shell prompts

Shell prompt on cs-class-1 (via ssh):

[re268@cs-class-1 ~]$

Shell prompt on my laptop (Ubuntu 20.04, WSL2)

rbessick5@Chinchilla:~$

Shell prompts are customizable but typically include the current user ("re268" or "rbessick5"), the name of the computer ("cs-class-1 or "Chinchilla") and the working directory of the shell.

You interact with the shell by typing commands at the shell prompt and pressing "Enter". These may be shell-specific keywords, but are often the names of programs you want to run. In this reference, we'll use "command" to refer to both shell keywords and Linux system programs.

You run some commands by typing their names. Other commands use additional information, called (command-line) arguments to control how they work. Arguments are typed after the command and are separated by spaces. Some commands need arguments to work, while others have a default behavior that optional arguments can change.

Linux commands often feature special arguments called options (or flags) that look like a single dash followed by a letter, or a double dash followed by a word.

Example: the date command

The date command prints the current date and time to the screen:

$ date
Thu Jan 21 12:57:17 EST 2021

By default, date uses the local time zone to display the date and time. You can use the option -u to print the current time in UTC:

$ date -u
Thu Jan 21 23:59:59 EST 2021

You'll get the same behavior with the options --utc or --universal. Many commands use one-letter flags as abbreviations for the longer form of commonly-used options.

As you learn to use Linux and bash, your vocabulary of basic commands will grow to include lots of tools for handling everyday tasks. There are a lot of Linux commands in part because each is designed to do a single, simple task, and in part because the shell lets you combine commands in a powerful way to do complicated tasks quickly.

The CS class server ("cs-class-1") is a Linux server can use while in a Georgetown CS course. You can access it remotely to get a bash shell prompt and work on assignments.

The class server is the target machine in many courses, meaning your programming projects are graded there. You'll want to be comfortable using bash there either to work on a project (say, with vim) or transfer code to/from the server (with scp and sftp to test and debug your homework.

Bash prompts usually end in a dollar sign ("$") for regular users. In this reference we'll write $ to mean "your shell prompt."

Bash prompts ending in a pound sign ("#") indicate the root user, a super-user with elevated permissions to run powerful commands.

Be EXTREMELY cautious running any command as root - especially a command you found on the Internet! Without warning or confirmation, root can perform actions denied to other users, including deleting your entire file system!

Reading the manual

As you use Linux more frequently, it's inevitable that you'll find yourself needing a command you've forgotten how to use - or one you've never used before. Luckily, Linux comes with a built-in instruction manual you can read without a Google search (remember, Linux is older than the World Wide Web!).

Image source: xkcd: RTFM by Randall Munroe (image link)

You can read the Linux manual using the man command. Every Linux command comes with a man page describing what the command does; its required and optional arguments; and the effects of those arguments.

man - an interface to the on-line reference manuals

$ man [man options] [[section] page]

The oldest Linux commands have short man pages, but more complicated tools can have man pages that are much longer than can fit in a single terminal window. You scroll through the man pages using your keyboard. The controls are simple, but not immediately obvious to modern users.

Navigating a man page

Basic commands while navigating a man page include:

  • h to open a list of controls
  • q to exit the man page (or exit the help page)
  • j to scroll down one line; k to scroll up one line.
  • f to scroll down one page; b to scroll up one page.
  • /pattern to search forward from your current place (the pattern can be a regular expression)
  • ?pattern to search backwards.
  • n to search forward for the next match (of your last search); N to search backward.

Example: man date

Earlier we saw the date command, which prints the current date and time to the terminal. You can see the man page for this command by typing:

$ man date

The first dozen lines of the man page are shown below (the full man page is about 200 lines).

Scroll farther down to display the optional arguments for date (there are many options for formatting how the date and time are printed).

You can even view the manual page for the man command itself - just type $ man man

This reference uses the same style as most man pages:

  • Bold text is typed exactly as shown.
  • Italic text is replaced with appropriate arguments.
  • [Brackets] indicate optional arguments (either typed as shown or replaced)
  • Ellipses (...) indicate an argument can be repeated (e.g., more than one filename).