> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/shell01/mac.md).

# MAC

**Objective:**

Write a shell script named `MAC.sh` that displays your machine’s **MAC addresses**. Each MAC address should be followed by a line break.

***

**Key Concepts:**

1. **MAC Addresses**: A MAC (Media Access Control) address is a unique identifier assigned to network interfaces for communication on a network. It is typically represented as six pairs of hexadecimal digits separated by colons (e.g., `00:1A:2B:3C:4D:5E`).
2. **`ifconfig` Command**: The `ifconfig` command is used to display and configure network interfaces on Unix-like systems.
3. **Regular Expressions**: Use `grep` with a regular expression to extract MAC addresses from the output of `ifconfig`.

***

**Script Implementation:**

Here’s the script you will use:

```bash
#!/usr/bin/env bash
ifconfig | grep -o -E '([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}'
```

***

**Explanation of the Script:**

Let’s break down the script line by line:

1. **`#!/usr/bin/env bash`**:
   * This is the **shebang**. It tells the system to use the `bash` shell to execute the script. Using `#!/usr/bin/env bash` is more portable than `#!/bin/bash` because it finds the `bash` interpreter in the user's environment.
2. **`ifconfig`**:
   * The `ifconfig` command displays information about all active network interfaces on your machine, including their MAC addresses.
   * Example output of `ifconfig` might look like this:

     ```
     eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
         inet6 fe80::1a2b:3c4d:5e6f:7a8b  prefixlen 64  scopeid 0x20<link>
         ether 00:1A:2B:3C:4D:5E  txqueuelen 1000  (Ethernet)
         RX packets 12345  bytes 1234567 (1.2 MB)
         TX packets 6789  bytes 987654 (987.6 KB)
     ```
3. **`grep -o -E '([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}'`**:
   * The `grep` command is used to search for patterns in text.
   * The `-o` option tells `grep` to only output the **matching part** of the line (in this case, the MAC address).
   * The `-E` option enables **extended regular expressions**.
   * The regular expression `([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}` matches MAC addresses:
     * `[0-9a-fA-F]{2}` matches exactly two hexadecimal digits (0-9, a-f, A-F).
     * `:` matches a colon.
     * `([0-9a-fA-F]{2}:){5}` matches five pairs of hexadecimal digits followed by colons.
     * `[0-9a-fA-F]{2}` matches the final pair of hexadecimal digits.
   * Example: If `ifconfig` outputs `ether 00:1A:2B:3C:4D:5E`, the `grep` command will extract `00:1A:2B:3C:4D:5E`.
4. **Output**:

   * The script will output all MAC addresses found in the `ifconfig` output, one per line.

   **Example Output**:

   ```
   00:1A:2B:3C:4D:5E
   00:1C:B3:4D:5E:6F
   ```

***

**Testing the Script:**

To test your script, follow these steps:

1. **Run the Script**:
   * Execute the script:

     ```
     ./MAC.sh
     ```
2. **Expected Output**:
   * The output will be a list of MAC addresses for all network interfaces on your machine, one per line. For example:

     Copy

     ```
     00:1A:2B:3C:4D:5E
     00:1C:B3:4D:5E:6F
     ```

***

**Why This Script Works:**

* The `ifconfig` command provides detailed information about network interfaces, including MAC addresses.
* The `grep` command with a regular expression extracts only the MAC addresses from the `ifconfig` output.
* The `-o` option ensures that only the MAC addresses are displayed, without any additional text.

***

**Additional Resources:**

* **`ifconfig` Command Documentation**: Learn more about the `ifconfig` command by typing `man ifconfig` or visiting the [Linux ifconfig documentation](https://linux.die.net/man/8/ifconfig).
* **Regular Expressions**: For more information on regular expressions, check out this [regular expression tutorial](https://www.regular-expressions.info/).
* **Bash Scripting Guide**: For more information on writing bash scripts, check out this [Bash scripting tutorial](https://ryanstutorials.net/bash-scripting-tutorial/).

***

**Key Takeaways:**

* You learned how to use the `ifconfig` command to retrieve network interface information.
* You practiced using `grep` with regular expressions to extract specific patterns (MAC addresses) from text.
* You wrote a script that automates the process of finding and displaying MAC addresses.

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://42-guide.gitbook.io/42-guide/piscine-life/shell01/mac.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.
