# System Variables in Bash

Absolutely! Let's enhance the chapter by including information about **positional parameters** (`$0`, `$1`, etc.) and how they are used in Bash scripts.

#### **Chapter 3: System Variables in Bash (Updated)**

**Objectives:**

* Understand what system variables are and their purpose.
* Learn about commonly used system variables.
* Explore how to create and use custom environment variables.
* Discuss positional parameters and their significance.
* Discuss the importance of system variables in cybersecurity tasks.

***

#### **3.1 What Are System Variables?**

System variables (also known as environment variables) are dynamic values that affect the behavior of processes on a computer. They are used to configure the environment in which scripts and programs run.

#### **3.2 Common System Variables**

Here are some commonly used system variables in Bash:

* **`$HOME`:** The home directory of the current user.
* **`$USER`:** The username of the current user.
* **`$PATH`:** A colon-separated list of directories that the shell searches for executable files.
* **`$PWD`:** The current working directory.
* **`$SHELL`:** The path to the current shell being used.
* **`$HOSTNAME`:** The name of the host machine.

You can view the value of these variables by using the `echo` command:

```bash
echo "Home directory: $HOME"
echo "Current user: $USER"
echo "Current shell: $SHELL"
```

***

#### **3.3 Positional Parameters**

Positional parameters are special variables that represent the arguments passed to a script. They are useful for passing input values when executing a script.

* **`$0`:** The name of the script.
* **`$1`, `$2`, `$3`, ...:** The first, second, third (and so on) arguments passed to the script.
* **`$#`:** The total number of arguments passed to the script.
* **`$@`:** All arguments passed to the script as separate quoted strings.
* **`$*`:** All arguments passed to the script as a single word.

**Example Script: `positional_params.sh`**

```bash
#!/bin/bash

# Display script name and arguments

echo "Script Name: $0"
echo "Number of Arguments: $#"

echo "Arguments Passed:"
for arg in "$@"; do
    echo "$arg"
done
```

**How to Run the Script**

1.  **Make the script executable:**

    ```bash
    chmod +x positional_params.sh
    ```
2.  **Run the script with arguments:**

    ```bash
    ./positional_params.sh arg1 arg2 arg3
    ```

***

#### **3.4 Creating Custom Environment Variables**

You can create your own environment variables to store data relevant to your scripts. This is done by using the following syntax:

```bash
MY_VAR="Hello, World!"
```

To access this variable, use the `$` symbol:

```bash
echo "$MY_VAR"
```

#### **3.5 Using System Variables in Scripts**

System variables can be utilized in scripts to make them dynamic and adaptable. Here's an updated example script that uses some system variables:

**Example Script: `system_info.sh`**

```bash
#!/bin/bash

# Display system information

echo "User Information:"
echo "Username: $USER"
echo "Home Directory: $HOME"
echo "Current Directory: $PWD"
echo "Shell: $SHELL"
echo "Hostname: $HOSTNAME"

# Check if the script is being run as root
if [ "$EUID" -eq 0 ]; then
    echo "You are running this script as root."
else
    echo "You are running this script as a non-root user."
fi

# Display script name and arguments (optional)
echo "Script Name: $0"
```

#### [**https://github.com/bc0de0/bash-scripts/blob/master/system\_info.sh**](https://github.com/bc0de0/bash-scripts/blob/master/system_info.sh)

#### **3.6 Importance of System Variables in Cybersecurity**

* **Configuration Management:** Environment variables can be used to manage configurations for different environments (development, staging, production) in your security tools and scripts.
* **Dynamic Scripts:** By using system variables and positional parameters, scripts can adapt to different users and systems without hardcoding values, making them reusable and flexible.
* **Security Practices:** When handling sensitive data (like API keys or passwords), it's a best practice to store such data in environment variables instead of hardcoding them in scripts.

#### **3.7 Summary**

In this chapter, you learned about:

* The purpose of system variables and their role in the Bash environment.
* Commonly used system variables and how to use them in scripts.
* Positional parameters and their significance in script execution.
* How to create and use custom environment variables.
* The relevance of these concepts in cybersecurity tasks.

#### **Exercises:**

1. Create a script that displays the current date and time using the `$DATE` system variable.
2. Modify the `system_info.sh` script to include the number of files in the current directory using the `$(ls | wc -l)` command.
3. Write a script that takes two arguments and prints them along with the script name.

***
