# Bash

Source: https://wisconsin.twango.dev/sp25-cs400/topics/bash

# Bash

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been distributed widely as the shell for the GNU operating system and as a default shell on Linux and OS X.

> [!tip] 
> Think of Bash as a program that takes in commands and executes them. It is a command-line interpreter that allows you to interact with your computer using text commands, but it can also run scripts that automate tasks.
>
## Basic Commands

### List

Lists files in the current directory.

```bash
foo@bar:~$ ls
```

Some flags that can be used with `ls`:

- `-l`: long listing format
- `-a`: list all files, including hidden files
- `-h`: human-readable file sizes

`ls` can also take a directory as an argument to list files in that directory.

```bash
ls /path/to/directory
```

### Print Working Directory

Prints the current working directory.

```bash
foo@bar:~$ pwd
```

### Echo

Prints a message to the terminal.

```bash
foo@bar:~$ echo "Hello, World!"
```

### Change Directory

Changes the current working directory.

```bash
foo@bar:~$ cd /path/to/directory
```

To move up one directory (the parent directory), use `..`.

```bash
foo@bar:~$ cd ..
```

To move to the home directory, use `~`.

```bash
foo@bar:~$ cd ~
```

### Make Directory

Creates a new directory.

```bash
foo@bar:~$ mkdir new_directory
```

### Copy

Copies files or directories.

```bash
foo@bar:~$ cp file1 file2
```

### Concatenate

Concatenates and displays the contents of files.

```bash
foo@bar:~$ cat file1 file2
```

> [!tip] 
> Most of the time to display the contents of a file, you can use `cat` or `less`. `cat` is used to display the entire file at once, while `less` is used to display the file one page at a time.
>
### Remove

Removes files or directories.

```bash
foo@bar:~$ rm file1
```

To remove a directory and its contents, use the `-r` flag (recursive).

```bash
foo@bar:~$ rm -r directory
```

> [!tip] 
> Typically, this is appended with the `-f` flag to force the removal of files without prompting for confirmation.
>
>
>
>

## Connections

- [[sp25-cs400/topics/index|Topic index]]

Obvious technical and formatting errors repaired.
