Lesson 3: Shell Navigation

Homepage Content Slides Video

Warning

This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.

The Shell

A shell is a text-based user-interface for a computer.
sea shell

Shells are the programs which allows you use a computer via text-based commands. Not to be confused with a terminal, which is the environment a shell runs in (GNOME terminal, tty1, etc). A shell is run in a terminal, a terminal is the widow you use the shell in.

Shell Examples

As with all things in Open Source, there are many options to choose from when deciding on a shell. Below is a short list of popular choices. Each shell has it’s positives and negatives. If you don’t have an optionion you should probably use what comes by default when you open your terminal.

sh
Required by all POSIX Operating Systems.
bash
Default on most GNU/Linux-based Operating Systems.
csh
Default shell on most BSD (Unix) based Operating Systems
fish
Useful but not sh compliant shell.
zsh
The hip new shell on the block.

Basic Shell Commands

The most basic job of a shell is to enable the user to execute programs and navigate the OS. Below are a few of the commands you will use to do this.

$ pwd    # Prints the current working directory (where you are)
$ ls     # Prints the contents of the current working directory
$ cd <path/to/other/directory>   # Navigates to a new directory.
$ echo "some thing $AND_VARS"    # Prints a string to the screen.
$ cat  foo.txt bax.txt # Prints the contents of a file(s) to the screen.
$ grep foo file.txt    # Searches `file.txt` for the string `foo`
$ less  file.txt       # Prints a file to the screen so you can arrow up/down.
$ env    # Prints environment variables to the screen.
$ whoami # Prints out current user
$ help   # When in doubt, always type help.

Shell Scripts

Shell scripts are files which contain command run on your behalf by the shell.

Each shell is also a programming langauge, so you can write logic, loops, functions, and declare local variables in your scripts.

about_me.sh

#!/bin/sh
if [ $(whoami) == "root" ]; then
  echo "You're root!"
else
  echo "Your username is $(whoami)"
  echo "Your home-directory is $HOME"
  echo "Your current directory is $PWD"
  echo "Your computer's host-name is $HOSTNAME"
fi

Invoke with:

$ chmod +x about_me.sh  # Tell Linux that this can be run as a program.
$ ./about_me.sh         # Invoke the script.

File Paths

File Paths describe the location of a file in the directory tree.

Files are an important part of using an OS through the shell. Being able to find, navigate to, and use files is important. The first part of doing that is understanding some basics about directories.

.
The current directory.
..
The parent directory.
~
Alias for your home directory.
/

Separates directories: one_dir/another_dir/last_dir

Alone, or at the start of a path, it is the root directory.

$ tree -F
.
|-- bar/
|   |-- one
|   `-- two
|-- baz/
`-- foo/
    `-- a/
        `-- b/

5 directories, 2 files

Special Characters

When using a shell (and in general) there are special characters. These serve a functional purpose (carry special meaning) in addition to being a character, so you can’t use them willy-nilly. You should know what those are and what they mean.

Wildcard (*)

Used as a stand-in for any character(s).

Example: cat *.log cats all files in the current working directory ending in .log.

End of line ($)
Used to specify the end of a regex. We’ll cover what regex is later.
Curl braces ({ })

Used to specify a set.

Example: ls {foo,bar,baz}ley-thing expands to ls fooley-thing barley-thing bazley-thing

Escape special characters (treat them as normal characters) with the escape character (\).

Type Less, Tab More

Pressing the tab key auto-completes a command, file-path, or argument in your shell.

Pressing tab multiple times completes the command to the best of the shells ability and then lists the possible completions (if there are any).

$ ls b    # <tab>
$ ls ba   # <tab>
bar_thing/ baz_thing/
$ ls bar  # <tab>
$ ls bar_thing

Further Reading

BASH Programming - Introduction HOW-TO
A free online resoruce of learning bash programming. Covers some concepts we’ll get to later in DOBC, but a good resoruce to have on hand.
Running rm -rf / on Linux
This video demonstrates what happens when you ‘delete your hard-drive’ on Linux. A fun watch!