> 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/print_groups.md).

# print\_groups

**Objective:**

Write a shell script named `print_groups.sh` that displays the list of groups for which the user (stored in the environment variable `FT_USER`) is a member. The output should be a **comma-separated list of group names**, without spaces or newlines.

***

**Key Concepts:**

1. **Environment Variables**: These are variables that are set in the shell and can be accessed by scripts. In this case, `FT_USER` is an environment variable that contains the username.
2. **`id` Command**: The `id` command is used to display user and group information for a specified user. It can show the groups a user belongs to.
3. **Text Manipulation**: You will need to manipulate the output of the `id` command to format it as a comma-separated list without spaces or newlines.

***

**Script Implementation:**

Here’s the script you will use:

```bash
#!/bin/sh
id -Gn $FT_USER | tr ' ' ',' | tr -d '\n'
```

***

**Explanation of the Script:**

Let’s break down the script line by line:

1. **`#!/bin/sh`**:
   * This is called a **shebang**. It tells the system that this script should be executed using the `/bin/sh` shell, which is a basic shell available on most Unix-like systems.
2. **`id -Gn $FT_USER`**:

   * The `id` command is used to retrieve user and group information.
   * The `-Gn` options do the following:
     * `-G`: Lists all the groups the user belongs to.
     * `-n`: Displays the group names instead of their numeric IDs.
   * `$FT_USER` is the environment variable that contains the username. The script uses this variable to get the group information for the specified user.

   **Example**: If `FT_USER=daemon`, the output of `id -Gn $FT_USER` might look like this:

   ```
   daemon bin
   ```
3. **`tr ' ' ','`**:

   * The `tr` command is used to **translate or replace characters**.
   * In this case, it replaces all spaces ( ) with commas (`,`).
   * After this step, the output from the previous command (`daemon bin`) becomes

   ```
   daemon,bin
   ```
4. **`tr -d '\n'`**:

   * The `tr -d` command is used to **delete specific characters**.
   * Here, it removes the newline character () from the output.
   * This ensures that the final output is a single line without any newlines.

   **Final Output**:

   ```
   daemon,bin
   ```

***

**Testing the Script:**

To test your script, follow these steps:

1. **Set the Environment Variable**:
   * First, set the `FT_USER` environment variable to a valid username on your system. For example:

     ```bash
     export FT_USER=daemon
     ```
2. **Run the Script**:
   * Execute the script:

     ```
     ./print_groups.sh
     ```
3. **Expected Output**:
   * If `FT_USER=daemon`, the output should be:

     ```
     daemon,bin
     ```

***

**Why This Script Works:**

* The `id -Gn` command retrieves the list of groups for the user.
* The `tr ' ' ','` command replaces spaces with commas to create a comma-separated list.
* The `tr -d '\n'` command removes any newline characters, ensuring the output is clean and on a single line.

***

**Additional Resources:**

* **`id` Command Documentation**: Learn more about the `id` command by typing `man id` in your terminal or visiting the [GNU Coreutils documentation](https://www.gnu.org/software/coreutils/manual/coreutils.html#id-invocation).
* **Environment Variables**: For more information on environment variables, check out this [guide on environment variables](https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/).
* **`tr` Command Documentation**: Learn more about the `tr` command by typing `man tr` or visiting the [GNU Coreutils documentation](https://www.gnu.org/software/coreutils/manual/coreutils.html#tr-invocation).

***

**Key Takeaways:**

* You learned how to use the `id` command to retrieve group information for a user.
* You practiced using the `tr` command to manipulate text output (replacing spaces with commas and removing newlines).
* You worked with environment variables to make your script dynamic and reusable.

This exercise is a great introduction to shell scripting and text manipulation. Keep practicing, and you'll get more comfortable with these concepts!


---

# 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/print_groups.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.
