# Introduction to the Terminal

## Introduction to the terminal/shell

> Nearly all platforms you can get your hands on have a shell in one form or another, and many of them have several shells for you to choose from. While they may vary in the details, at their core they are all roughly the same: they allow you to run programs, give them input, and inspect their output in a semi-structured way.
>
> \-- Excerpt from the Missing Semester

For the most part, when people talk about the terminal, we normally talk about Bash, or POSIX-compliant shells. POSIX is just a fancy name for a set of rules that a shell should abide by, so that different shells can have similar behaviour. Other shells include:

* fish
* zsh
* nushell
* powershell

In modern operating systems, to open a shell prompt, you often need a terminal. Think of it as a nice GUI wrapping the textual interface (the shell). Your device should probably be shipped with one, or you should be able to install one easily.

### The Shell Prompt

When you first launch a terminal, you will see a *prompt, similar to or a slight variation of*:

```
chun@legion:~$
```

This prompt tells you for example, that your username is `chun` on the machine named `legion`, and that you 'working directory' is `~` (short for home, we'll get to that). You should also see a blinking cursor, which you can type anything to, and when you hit `Enter`, it should execute the command you've typed out.

Here are some really simple commands you should be able to run (but some may not be installed depending on your machine):

* `date` - shows the date

```shell-session
Sun Sep  8 08:23:13 PM +08 2024`
```

* `cal` - a tiny calendar

```shell-session
   September 2024   
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30               
              
```

* `uptime` - shows how long your computer has been powered on

```shell-session
 20:26:04  up   1:01,  2 users,  load average: 0.45, 0.62, 0.62
```

* `echo` - echoes what you typed

```shell-session
chun@legion:~$ echo hello
hello
```

### Folders and directories

These commands are neat, but we can't really do much in the command line until we understand the concept of folders and directories.&#x20;

All your files and directories on your system are stored in a structure known as a tree. This tree starts from a 'root' directory, this will be `/` on Linux and MacOS, and something like `C:\` on Windows.&#x20;

A **path** on the shell is just a list of directories, seperated by `/` on Linux and macOS and `\` on Windows. For example:

```
C:\User\user\Documents - For Windows
~/Downloads - For Linux or MacOS
```

There are two types of paths:

* The absolute path is a path that starts from the root directory
* Relative paths are relative to your *current working directory,* or where you shell currently is

To see where you current directory is, use **print working directory,** or `pwd` for short in the terminal.

In a path, `.` refers to the current directory, and `..` refers to the parent directory.

### Common Commands

* to get the **man**ual pages of a command
* to **c**hange **d**irectory
* to **l**i**s**t files and directories
* to **r**e**m**ove files and directories
* to **c**o**p**y file
* to **m**o**v**e file
* to **p**rint **w**orking **d**irectory

### Navigating the shell

`bash` has shortcuts that are based on `emacs` keybindings:

* `Ctrl` + `a` - beginning of line
* `Ctrl` + `e` - end of line
* `Alt` + `b` - move back one word
* `Alt` + `f` - move forward one word
* `Ctrl` + `k` - delete from cursor to end of line
* `Ctrl` + `_` - undo

And some special keybindings:

* `Ctrl` + `u` - delete from cursor to the start of line
* `Ctrl` + `w` - delete from cursor to the start of word
* `Ctrl` + `c` - terminates the command
* `Ctrl` + `z` - suspends the command
* `Ctrl` + `l` - clears the screen
* `Ctrl` + `s` - stops the output to the screen
* `Ctrl` + `q` - allows output to the screen

You can find even more by doing `man readline`

{% embed url="<https://www.man7.org/linux/man-pages/man3/readline.3.html>" %}

### Editing in the terminal

There are a few ways you can make changes to your filesystem, be it editing files or directories/folders:

* `mkdir` to make a new empty directory
* `touch <filename>` to make a new empty file
* `nano <filename>` to open a editor to edit the file
  * `Ctrl` + `o` - to save
  * `Ctrl` + `x` - to exit

### Finding commands

What if we want to find our previously used commands? If you haven't already realized, you should be able to use the up arrow to scroll through your previous commands, but it's not very efficient. This is where we can use a command called `history`.

```shell-session
chun@legion:~$ history
```

### Composability

Remember what we said about the Unix Philosophy? A big part about it is the idea of programs working well together. The terminal allows this by allow the output of one program to be the input of another program. This is known as **piping.** To pipe we can do something like:

```bash
history | grep "echo"
```

* `history` prints out the entire command history as the output
* `grep` takes in an input and tries to filter for the keyword "echo"

Some other ways you could use pipes:

```bash
history | head ## Grabs the first 10 lines of the output
history | tail ## Grabs the last 10 lines of the output
## Opens output in a scrollable format, use you arrow keys to navigate
## and press q to quit
history | less 
```

There are more ways we can compose programs, which we'll go through in the scripting section!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.nushackers.org/hackerschool/beginners-guide-to-the-terminal/introduction-to-the-terminal.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
