{"id":23382259,"url":"https://github.com/adamo08/bash-scripting-basics","last_synced_at":"2026-02-03T06:03:10.136Z","repository":{"id":259117563,"uuid":"875257387","full_name":"Adamo08/bash-scripting-basics","owner":"Adamo08","description":"Beginner's guide to shell scripting with examples and explanations.","archived":false,"fork":false,"pushed_at":"2024-10-21T21:17:49.000Z","size":19,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T08:49:19.470Z","etag":null,"topics":["bash","bash-scripting","command-line","linux","shell","tutorial","unix"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Adamo08.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-19T14:01:28.000Z","updated_at":"2025-04-07T11:14:31.000Z","dependencies_parsed_at":"2024-10-22T22:29:46.998Z","dependency_job_id":null,"html_url":"https://github.com/Adamo08/bash-scripting-basics","commit_stats":null,"previous_names":["adamo08/bash-scripting-basics"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Adamo08/bash-scripting-basics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adamo08%2Fbash-scripting-basics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adamo08%2Fbash-scripting-basics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adamo08%2Fbash-scripting-basics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adamo08%2Fbash-scripting-basics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Adamo08","download_url":"https://codeload.github.com/Adamo08/bash-scripting-basics/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Adamo08%2Fbash-scripting-basics/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29035259,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T02:28:16.591Z","status":"ssl_error","status_checked_at":"2026-02-03T02:27:48.904Z","response_time":96,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","bash-scripting","command-line","linux","shell","tutorial","unix"],"created_at":"2024-12-21T21:19:12.818Z","updated_at":"2026-02-03T06:03:10.105Z","avatar_url":"https://github.com/Adamo08.png","language":"Shell","readme":"---\n\n# **Beginner's Guide to Shell Scripting**\n\nThis is a **full beginner's course** on **shell scripting** that will take you from the very basics to writing simple scripts and connecting them in Unix-based systems.\n\n---\n\n## **Table of Contents**\n\n1. [Introduction to the Shell](#1-introduction-to-the-shell)\n   - [What is a Shell?](#what-is-a-shell)\n   - [Types of Shells](#types-of-shells)\n   - [Navigating the Terminal](#navigating-the-terminal)\n   - [Basic Commands](#basic-commands)\n2. [Writing Your First Shell Script](#2-writing-your-first-shell-script)\n   - [What is a Shell Script?](#what-is-a-shell-script)\n   - [How to Create and Run a Script](#how-to-create-and-run-a-script)\n   - [Making Scripts Executable](#making-scripts-executable)\n   - [Comments and Documentation](#comments-and-documentation)\n3. [Variables in Shell](#3-variables-in-shell)\n   - [Defining Variables](#defining-variables)\n   - [Using Variables](#using-variables)\n   - [Special Variables](#special-variables)\n   - [Quoting](#quoting)\n4. [Conditionals and Control Flow](#4-conditionals-and-control-flow)\n   - [`if` Statements](#if-statements)\n   - [Using Conditions (`test`, `[ ]`, `[[ ]]`)](#using-conditions-test)\n   - [`case` Statements](#case-statements)\n5. [Loops](#5-loops)\n   - [`for` Loops](#for-loops)\n   - [`while` Loops](#while-loops)\n   - [`until` Loops](#until-loops)\n   - [Breaking Out of Loops](#breaking-out-of-loops)\n6. [Functions in Shell Scripts](#6-functions-in-shell-scripts)\n   - [Defining Functions](#defining-functions)\n   - [Calling Functions](#calling-functions)\n   - [Passing Arguments to Functions](#passing-arguments-to-functions)\n7. [Input and Output](#7-input-and-output)\n   - [Echoing Text](#echoing-text)\n   - [Reading User Input](#reading-user-input)\n   - [Redirecting Input and Output](#redirecting-input-and-output)\n   - [Using `cat`, `head`, `tail`](#using-cat-head-tail)\n8. [Working with Files](#8-working-with-files)\n   - [Creating, Moving, and Deleting Files](#creating-moving-and-deleting-files)\n   - [File Permissions and Ownership](#file-permissions-and-ownership)\n   - [Using Wildcards (`*`, `?`)](#using-wildcards)\n   - [File Tests (`-f`, `-d`, etc.)](#file-tests)\n9. [Working with Strings and Numbers](#9-working-with-strings-and-numbers)\n   - [String Manipulation](#string-manipulation)\n   - [Arithmetic in Shell](#arithmetic-in-shell)\n10. [Advanced Scripting Techniques](#10-advanced-scripting-techniques)\n    - [`sed` and `awk` for Text Manipulation](#sed-and-awk-for-text-manipulation)\n    - [Using Regular Expressions in Shell Scripts](#using-regular-expressions-in-shell-scripts)\n    - [Scheduling Tasks with `cron`](#scheduling-tasks-with-cron)\n    - [Debugging Shell Scripts](#debugging-shell-scripts)\n11. [Additional Topics](#11-additional-topics)\n    - [Error Handling and Exit Status](#error-handling-and-exit-status)\n    - [Shell Script Arguments Parsing](#shell-script-arguments-parsing)\n    - [Environment Variables](#environment-variables)\n    - [Trap and Signal Handling](#trap-and-signal-handling)\n\n---\n\n## **1. Introduction to the Shell**\n\n### **What is a Shell?**\n\nA **shell** is a command-line interpreter that allows users to interact with the operating system. It processes user commands and requests the operating system to perform the specified tasks.\n\n### **Types of Shells**\n\n| Shell | Description |\n|-------|-------------|\n| **Bash** | Bourne Again Shell - Default on many Unix-based systems. |\n| **Zsh**  | Z Shell - Offers improvements over Bash. |\n| **Fish** | Friendly Interactive Shell - User-friendly features. |\n| **Ksh**  | Korn Shell - Combines features of Bourne and C Shells. |\n\nTo check which shell you're using, type:\n\n```bash\necho $SHELL\n```\n\n### **Navigating the Terminal**\n\nBasic navigation commands:\n\n| Command | Description |\n|---------|-------------|\n| `pwd`   | Show current directory. |\n| `cd`    | Change directory. |\n| `ls`    | List files in a directory. |\n\n### **Basic Commands**\n\n#### **Example Script: Basic Commands**\n\n```bash\n#!/bin/bash\n\n# Display current directory\necho \"Current Directory: $(pwd)\"\n\n# List files\necho \"Files in Directory:\"\nls\n```\n\n---\n\n## **2. Writing Your First Shell Script**\n\n### **What is a Shell Script?**\n\nA **shell script** is a text file containing a sequence of commands for the shell to execute.\n\n### **How to Create and Run a Script**\n\n#### **Example Script: Hello World**\n\n```bash\n#!/bin/bash\necho \"Hello, World!\"\n```\n\n**Steps:**\n\n1. **Create the script file:**\n\n   ```bash\n   nano hello_world.sh\n   ```\n\n2. **Add the content above and save the file.**\n\n3. **Make the script executable:**\n\n   ```bash\n   chmod +x hello_world.sh\n   ```\n\n4. **Run the script:**\n\n   ```bash\n   ./hello_world.sh\n   ```\n\n### **Comments and Documentation**\n\nUse `#` for comments.\n\n```bash\n#!/bin/bash\n\n# This script prints Hello, World!\necho \"Hello, World!\"\n```\n\n---\n\n## **3. Variables in Shell**\n\n### **Defining Variables**\n\n#### **Example Script: Defining Variables**\n\n```bash\n#!/bin/bash\n\nNAME=\"Adamo\"\nAGE=21\n```\n\n### **Using Variables**\n\n```bash\necho \"My name is $NAME and I am $AGE years old.\"\n```\n\n#### **Complete Script: Using Variables**\n\n```bash\n#!/bin/bash\n\nNAME=\"Adamo\"\nAGE=21\n\necho \"My name is $NAME and I am $AGE years old.\"\n```\n\n### **Special Variables**\n\n| Variable | Description |\n|----------|-------------|\n| `$0`     | The name of the script. |\n| `$1` - `$9` | The first to ninth arguments to the script. |\n| `$@`     | All the arguments. |\n| `$#`     | Number of arguments. |\n| `$$`     | Process ID of the script. |\n| `$?`     | Exit status of the last command. |\n\n#### **Example Script: Special Variables**\n\n```bash\n#!/bin/bash\n\necho \"Script name: $0\"\necho \"First argument: $1\"\necho \"All arguments: $@\"\necho \"Number of arguments: $#\"\n```\n\n### **Quoting**\n\n| Quote Type | Description |\n|------------|-------------|\n| `' '` (Single Quotes) | Prevents variable expansion. |\n| `\" \"` (Double Quotes) | Allows variable expansion. |\n| `` ` ` `` (Backticks) | Executes commands inside. |\n\n#### **Example Script: Quoting**\n\n```bash\n#!/bin/bash\n\nNAME=\"Adamo\"\n\necho 'Hello $NAME'    # Outputs: Hello $NAME\necho \"Hello $NAME\"    # Outputs: Hello Adamo\n\nDATE=`date`\necho \"Today is $DATE\"\n```\n\n---\n\n## **4. Conditionals and Control Flow**\n\n### **`if` Statements**\n\n#### **Syntax:**\n\n```bash\nif [ condition ]; then\n  # commands\nfi\n```\n\n#### **Example Script: Simple `if` Statement**\n\n```bash\n#!/bin/bash\n\nNAME=\"Adamo\"\n\nif [ \"$NAME\" == \"Adamo\" ]; then\n  echo \"Hello Adamo!\"\nfi\n```\n\n### **`else` and `elif` Statements**\n\n#### **Syntax:**\n\n```bash\nif [ condition ]; then\n  # commands\nelif [ condition ]; then\n  # commands\nelse\n  # commands\nfi\n```\n\n#### **Example Script: `if-elif-else`**\n\n```bash\n#!/bin/bash\n\nNAME=$1  # Get the first argument\n\nif [ \"$NAME\" == \"Adamo\" ]; then\n  echo \"Hello Adamo!\"\nelif [ \"$NAME\" == \"Sofia\" ]; then\n  echo \"Hello Sofia!\"\nelse\n  echo \"Hello Stranger!\"\nfi\n```\n\n### **Using Conditions (`test`, `[ ]`, `[[ ]]`)**\n\n\n`test`, `[ ]`, and `[[ ]]` are used to evaluate conditions in shell scripts, but they have some differences:\n\n#### **1. `test` and `[ ]`**\n\n- `test` is a command that evaluates conditions. It can also be written using the `[ ]` shorthand.\n- Both `test` and `[ ]` support basic comparisons and are POSIX-compliant, meaning they work on all Unix-like systems.\n- Example:\n\n```bash\nif test \"$NUM1\" -eq \"$\n\nNUM2\"; then\n  echo \"Numbers are equal\"\nfi\n\n# Equivalent to\nif [ \"$NUM1\" -eq \"$NUM2\" ]; then\n  echo \"Numbers are equal\"\nfi\n```\n\n#### **2. `[[ ]]`**\n\n- `[[ ]]` is an enhanced version of `[ ]` with additional functionality, such as pattern matching and better support for complex expressions.\n- It is **bash-specific** (not POSIX), meaning it won't work on all shells, but it is more powerful.\n- Example:\n\n```bash\nif [[ \"$STRING\" =~ ^[a-z]+$ ]]; then\n  echo \"String is lowercase\"\nfi\n```\n\n#### **3. Key Differences:**\n\n- `[[ ]]` allows for more complex conditionals and doesn’t require escaping certain characters, while `[ ]` is more limited and requires careful quoting and escaping.\n- `[[ ]]` is considered safer when using string comparisons because it prevents many unexpected issues caused by shell expansions.\n\n#### **Example Script: Comparing Numbers and Strings**\n\n```bash\n#!/bin/bash\n\nNUM1=10\nNUM2=20\n\nif [ \"$NUM1\" -lt \"$NUM2\" ]; then\n  echo \"$NUM1 is less than $NUM2\"\nfi\n\nSTR=\"hello\"\n\nif [[ \"$STR\" =~ ^[a-z]+$ ]]; then\n  echo \"$STR contains only lowercase letters\"\nfi\n```\n\n\n#### **Comparison Operators:**\n\n| Operator | Description | For Numbers | For Strings |\n|----------|-------------|-------------|-------------|\n| `-eq`    | Equal            | ✔️ |  |\n| `-ne`    | Not equal        | ✔️ |  |\n| `-gt`    | Greater than     | ✔️ |  |\n| `-lt`    | Less than        | ✔️ |  |\n| `-ge`    | Greater or equal | ✔️ |  |\n| `-le`    | Less or equal    | ✔️ |  |\n| `=`      | Equal            |  | ✔️ |\n| `!=`     | Not equal        |  | ✔️ |\n| `-z`     | String is empty  |  | ✔️ |\n| `-n`     | String is not empty |  | ✔️ |\n\n#### **Example Script: Using Conditions**\n\n```bash\n#!/bin/bash\n\nNUM1=10\nNUM2=20\n\nif [ $NUM1 -lt $NUM2 ]; then\n  echo \"$NUM1 is less than $NUM2\"\nfi\n\nSTR=\"Hello\"\n\nif [ -n \"$STR\" ]; then\n  echo \"String is not empty\"\nfi\n```\n\n### **`case` Statements**\n\n#### **Syntax:**\n\n```bash\ncase variable in\n  pattern1)\n    # commands\n    ;;\n  pattern2)\n    # commands\n    ;;\n  *)\n    # default commands\n    ;;\nesac\n```\n\n#### **Example Script: `case` Statement**\n\n```bash\n#!/bin/bash\n\nCOLOR=$1  # Get the color from the first argument\n\ncase $COLOR in\n  \"red\")\n    echo \"The color is red.\"\n    ;;\n  \"blue\")\n    echo \"The color is blue.\"\n    ;;\n  \"green\")\n    echo \"The color is green.\"\n    ;;\n  *)\n    echo \"Unknown color.\"\n    ;;\nesac\n```\n\n---\n\n## **5. Loops**\n\n### **`for` Loops**\n\n#### **Syntax:**\n\n```bash\nfor variable in list; do\n  # commands\ndone\n```\n\n#### **Example Script: `for` Loop**\n\n```bash\n#!/bin/bash\n\nfor i in {1..5}; do\n  echo \"Number: $i\"\ndone\n```\n\n### **`while` Loops**\n\n#### **Syntax:**\n\n```bash\nwhile [ condition ]; do\n  # commands\ndone\n```\n\n#### **Example Script: `while` Loop**\n\n```bash\n#!/bin/bash\n\ni=1\nwhile [ $i -le 5 ]; do\n  echo \"Number: $i\"\n  i=$((i + 1))\ndone\n```\n\n### **`until` Loops**\n\n#### **Syntax:**\n\n```bash\nuntil [ condition ]; do\n  # commands\ndone\n```\n\n#### **Example Script: `until` Loop**\n\n```bash\n#!/bin/bash\n\ni=1\nuntil [ $i -gt 5 ]; do\n  echo \"Number: $i\"\n  i=$((i + 1))\ndone\n```\n\n### **Breaking Out of Loops**\n\nUse `break` and `continue` to control loop execution.\n\n#### **Example Script: Break and Continue**\n\n```bash\n#!/bin/bash\n\nfor i in {1..10}; do\n  if [ $i -eq 5 ]; then\n    echo \"Skipping number 5\"\n    continue\n  fi\n  if [ $i -gt 8 ]; then\n    echo \"Breaking at number $i\"\n    break\n  fi\n  echo \"Number: $i\"\ndone\n```\n\n---\n\n## **6. Functions in Shell Scripts**\n\n### **Defining Functions**\n\n#### **Syntax:**\n\n```bash\nfunction_name() {\n  # commands\n}\n```\n\n#### **Example Script: Defining a Function**\n\n```bash\n#!/bin/bash\n\ngreet() {\n  echo \"Hello, $1!\"\n}\n```\n\n### **Calling Functions**\n\n```bash\ngreet \"Adamo\"\n```\n\n### **Passing Arguments to Functions**\n\n#### **Complete Script: Functions with Arguments**\n\n```bash\n#!/bin/bash\n\ngreet() {\n  echo \"Hello, $1!\"\n}\n\ngreet \"Adamo\"\ngreet \"Sofia\"\n```\n\n---\n\n## **7. Input and Output**\n\n### **Echoing Text**\n\n```bash\n#!/bin/bash\n\necho \"This is a message.\"\n```\n\n### **Reading User Input**\n\n#### **Example Script: Reading Input**\n\n```bash\n#!/bin/bash\n\nread -p \"Enter your name: \" NAME\necho \"Hello, $NAME!\"\n```\n\n### **Redirecting Input and Output**\n\n| Symbol | Description |\n|--------|-------------|\n| `\u003e`    | Redirect output to a file (overwrite). |\n| `\u003e\u003e`   | Redirect output to a file (append). |\n| `\u003c`    | Redirect input from a file. |\n| `2\u003e`   | Redirect stderr to a file. |\n| `\u0026\u003e`   | Redirect both stdout and stderr to a file. |\n\n#### **Example Script: Redirecting Output**\n\n```bash\n#!/bin/bash\n\nls -l \u003e files_list.txt\necho \"Directory listing saved to files_list.txt\"\n```\n\n---\n\n## **8. Working with Files**\n\n### **Creating, Moving, and Deleting Files**\n\n#### **Example Script: File Operations**\n\n```bash\n#!/bin/bash\n\n# Create a file\ntouch myfile.txt\n\n# Move the file\nmv myfile.txt mynewfile.txt\n\n# Copy the file\ncp mynewfile.txt copy_of_mynewfile.txt\n\n# Delete the files\nrm mynewfile.txt copy_of_mynewfile.txt\n```\n\n### **File Permissions and Ownership**\n\nChange file permissions using `chmod`.\n\n#### **Example Script: Changing Permissions**\n\n```bash\n#!/bin/bash\n\n# Give read, write, and execute permissions to the user\nchmod u+rwx script.sh\n\n# Remove write permission from group and others\nchmod go-w script.sh\n```\n\n### **Using Wildcards (`*`, `?`)**\n\n- `*` matches any number of characters.\n- `?` matches a single character.\n\n#### **Example Script: Using Wildcards**\n\n```bash\n#!/bin/bash\n\n# List all .txt files\nls *.txt\n\n# List files starting with 'file' and any single character\nls file?.txt\n```\n\n### **File Tests**\n\n| Test Expression | Description |\n|-----------------|-------------|\n| `-e FILE`       | File exists. |\n| `-f FILE`       | File exists and is a regular file. |\n| `-d DIRECTORY`  | Directory exists. |\n| `-s FILE`       | File is not empty. |\n| `-r FILE`       | File is readable. |\n| `-w FILE`       | File is writable. |\n| `-x FILE`       | File is executable. |\n\n#### **Example Script: File Tests**\n\n```bash\n#!/bin/bash\n\nFILE=\"testfile.txt\"\n\nif [ -e \"$FILE\" ]; then\n  echo \"$FILE exists.\"\nelse\n  echo \"$FILE does not exist.\"\nfi\n\nif [ -f \"$FILE\" ]; then\n  echo \"$FILE is a regular file.\"\nfi\n\nif [ -s \"$FILE\" ]; then\n  echo \"$FILE is not empty.\"\nfi\n```\n\n---\n\n## **9. Working with Strings and Numbers**\n\n### **String Manipulation**\n\n#### **Extract Substring**\n\n```bash\n#!/bin/bash\n\nSTR=\"Hello World\"\nSUBSTR=${STR:0:5}\necho \"Substring: $SUBSTR\"  # Outputs: Hello\n```\n\n#### **String Length**\n\n```bash\n#!/bin/bash\n\nSTR=\"Hello\"\nLENGTH=${#STR}\necho \"Length: $LENGTH\"  # Outputs: 5\n```\n\n### **Arithmetic in Shell**\n\nUse `$(( expression ))` for arithmetic operations.\n\n#### **Example Script: Arithmetic Operations**\n\n```bash\n#!/bin/bash\n\nNUM1=10\nNUM2=5\n\nSUM=$((NUM1 + NUM2))\nDIFF=$((NUM1 - NUM2))\nPROD=$((NUM1 * NUM2))\nQUOT=$((NUM1 / NUM2))\nMOD=$((NUM1 % NUM2))\n\necho \"Sum: $SUM\"\necho \"Difference: $DIFF\"\necho \"Product: $PROD\"\necho \"Quotient: $QUOT\"\necho \"Remainder: $MOD\"\n```\n\n---\n\n## **10. Advanced Scripting Techniques**\n\n### **`sed` and `awk` for Text Manipulation**\n\n#### **Using `sed` for Search and Replace**\n\n```bash\n#!/bin/bash\n\n# Replace 'old' with 'new' in file.txt\nsed 's/old/new/g' file.txt \u003e newfile.txt\n```\n\n#### **Using `awk` for Field Extraction**\n\n```bash\n#!/bin/bash\n\n# Print the first column of a file\nawk '{print $1}' file.txt\n```\n\n### **Using Regular Expressions in Shell Scripts**\n\n#### **Example Script: Validating Email with Regex**\n\n```bash\n#!/bin/bash\n\nread -p \"Enter your email: \" EMAIL\n\nif [[ $EMAIL =~ ^[a-zA-Z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$ ]]; then\n  echo \"Valid email.\"\nelse\n  echo \"Invalid email.\"\nfi\n```\n\n### **Scheduling Tasks with `cron`**\n\nTo schedule tasks, edit the crontab file.\n\n#### **Example: Running a Script Every Day at 2 AM**\n\n1. Open the crontab editor:\n\n   ```bash\n   crontab -e\n   ```\n\n2. Add the following line:\n\n   ```bash\n   0 2 * * * /path/to/script.sh\n   ```\n\n### **Debugging Shell Scripts**\n\nUse `-x` option to trace script execution.\n\n#### **Example: Debugging a Script**\n\n```bash\nbash -x script.sh\n```\n\n---\n\n## **11. Additional Topics**\n\n### **Error Handling and Exit Status**\n\nEvery command returns an exit status (0 for success, non-zero for failure).\n\n#### **Example Script: Checking Exit Status**\n\n```bash\n#!/bin/bash\n\nmkdir new_directory\n\nif [ $? -eq 0 ]; then\n  echo \"Directory created successfully.\"\nelse\n  echo \"Failed to create directory.\"\nfi\n```\n\nAlternatively, use `set -e` to exit the script if any command fails.\n\n### **Shell Script Arguments Parsing**\n\nUse `getopts` for parsing options.\n\n#### **Example Script: Using `getopts`**\n\n```bash\n#!/bin/bash\n\nwhile getopts \":u:p:\" opt; do\n  case $opt in\n    u) USERNAME=\"$OPTARG\"\n    ;;\n    p) PASSWORD=\"$OPTARG\"\n    ;;\n    \\?) echo \"Invalid option -$OPTARG\" \u003e\u00262\n    ;;\n  esac\ndone\n\necho \"Username: $USERNAME\"\necho \"Password: $PASSWORD\"\n```\n\nRun the script:\n\n```bash\n./script.sh -u \"adamo\" -p \"password123\"\n```\n\n### **Environment Variables**\n\nEnvironment variables are available to all child processes.\n\n#### **Example Script: Accessing Environment Variables**\n\n```bash\n#!/bin/bash\n\necho \"Home Directory: $HOME\"\necho \"Current User: $USER\"\necho \"Path Variable: $PATH\"\n```\n\n### **Trap and Signal Handling**\n\nHandle signals using `trap`.\n\n#### **Example Script: Handling SIGINT**\n\n```bash\n#!/bin/bash\n\ntrap \"echo 'Script interrupted! Exiting...'; exit 1\" SIGINT\n\necho \"Press Ctrl+C to interrupt.\"\n\nwhile true; do\n  sleep 1\ndone\n```\n\n---\n\n## **Conclusion**\n\nThis guide covers the fundamentals of shell scripting, providing examples for each topic to help you practice and understand how shell scripts work. By working through these examples, you'll gain a solid foundation in shell scripting and be prepared to tackle more complex scripting tasks.\n\n---\n\n**Happy Scripting!**\n\n# **Additional Resources**\n\n- **Bash Reference Manual**: https://www.gnu.org/software/bash/manual/bash.html\n- **Advanced Bash-Scripting Guide**: http://tldp.org/LDP/abs/html/\n\n---","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamo08%2Fbash-scripting-basics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadamo08%2Fbash-scripting-basics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamo08%2Fbash-scripting-basics/lists"}