# Cheat Sheet #day52 - echo

# `echo` Command Cheatsheet

The `echo` command in Unix/Linux is used to display a line of text or a string that is passed as an argument. It is commonly used in shell scripts and command-line operations to output text to the terminal or to a file.

## Syntax

```bash
echo [OPTION]... [STRING]...
```

## Common Options

* `-n`: Do not output the trailing newline.
    
* `-e`: Enable interpretation of backslash escapes.
    
* `-E`: Disable interpretation of backslash escapes (default).
    

## Backslash Escapes (used with `-e`)

* `\a`: Alert (bell)
    
* `\b`: Backspace
    
* `\c`: Suppress trailing newline
    
* `\e`: Escape character
    
* `\f`: Form feed
    
* `\n`: New line
    
* `\r`: Carriage return
    
* `\t`: Horizontal tab
    
* `\v`: Vertical tab
    
* `\\`: Backslash
    
* `\0nnn`: Byte with octal value nnn (1 to 3 digits)
    
* `\xHH`: Byte with hexadecimal value HH (1 to 2 digits)
    

## Examples and Use Cases

### Basic Usage

```bash
echo "Hello, World!"
```

* Outputs: `Hello, World!`
    

### Suppress Newline

```bash
echo -n "Hello, World!"
```

* Outputs: `Hello, World!` without trailing newline.
    

### Enable Backslash Escapes

```bash
echo -e "Line 1\nLine 2\nLine 3"
```

* Outputs:
    
    ```plaintext
    Line 1
    Line 2
    Line 3
    ```
    

### Include a Tab

```bash
echo -e "Column 1\tColumn 2"
```

* Outputs: `Column 1 Column 2` with a tab space between the columns.
    

### Carriage Return

```bash
echo -e "Hello\rWorld"
```

* Outputs: `World` (the `\r` causes `Hello` to be overwritten by `World`).
    

### Alert/Bell

```bash
echo -e "\a"
```

* Produces an alert sound if the terminal supports it.
    

### Output to a File

```bash
echo "This is a test" > file.txt
```

* Writes `This is a test` to `file.txt`, overwriting the file if it exists.
    

### Append to a File

```bash
echo "This is an additional line" >> file.txt
```

* Appends `This is an additional line` to `file.txt`.
    

### Using Variables

```bash
name="Alice"
echo "Hello, $name!"
```

* Outputs: `Hello, Alice!`
    

### Combining Text and Command Output

```bash
echo "Current directory is $(pwd)"
```

* Outputs: `Current directory is /path/to/current/directory`
    

---

This cheatsheet provides a quick reference to the most common usages and options for the `echo` command. For more detailed information, you can always refer to the `echo` man page by typing `man echo` in your terminal.
