hexdefender
Email
  • Introduction to Linux
    • Overview
    • Linux Kernel
    • Linux Distros
    • Introduction to Kali Linux
    • Install Kali on VirtualBox
    • Install Kali on AWS
  • Linux Commands
    • Linux File Systems
    • Basic File and Directory commands
    • File Permissions and Ownerships
    • System Commands in Linux
    • Text Processing Commands in Linux
    • Linux Archive Utility
    • Package Management in Kali Linux
    • Networking Commands
    • Disk Utility Tools
    • Linux List of CLI Command lookup
    • Linux CLI Cheatsheet
    • Assignment
  • Networking Essentials
    • Overview
    • Networking Protocols
    • IP Addressing & Subnetting
    • DNS and DNS Security
    • Network Devices and Architecture
    • VPNs and Secure Tunnels
    • Network Address Translation (NAT) & Port Forwarding
    • Wireless Networks & Protocols
    • Cloud Networking & Security
    • Common Network Tools
  • Bash Scripting
    • Fundamentals of Bash
    • Variables, Branching and Loops
    • System Variables in Bash
    • Functions and Error Handling in Bash Scripts
    • File Handling and Text Processing
    • 5 Useful Bash Scripts for Everyday Tasks
    • Useful Assignments
  • Fundamentals of Cybersecurity
    • Introduction to Cybersecurity
    • Importance of Cybersecurity
    • Important Cybersecurity Frameworks
    • Cybersecurity Roles and Career Options
  • Penetration Testing
    • Reconnaissance and Footprinting
    • Exploitation Techniques
      • Introduction
      • Service Enumeration
      • Password Attacks
      • Exploit Discovery
      • The Art of Exploitation
      • The Pentester's guide to Metasploit
    • Post Exploitation - Malware & Escalation
  • Web Application Security
    • Common Web Vulnerabilities
    • OWASP Top 10
    • SQL Injections
    • Cross Site Scripting Attacks
    • Web Application Firewalls
    • Secure Coding Practices
  • Cryptography
    • Basic concepts of cryptography
    • Examples of Asymetric & Hashing functions
    • Public Key Infrastructure
    • Digital Signatures
    • Symmetric and Asymmetric Encryption
  • Social Engineering
    • Introduction to Social Engineering
    • Mitigation Strategies for Social Engineering
  • Digital Forensics
    • Digital Forensics Basics
    • Forensics Tools and Techniques
    • Reverse Engineering Fundamentals
    • Malware Analysis
Powered by GitBook
On this page
  1. Bash Scripting

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:

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

#!/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:

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

    ./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:

MY_VAR="Hello, World!"

To access this variable, use the $ symbol:

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

#!/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"

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.


PreviousVariables, Branching and LoopsNextFunctions and Error Handling in Bash Scripts

Last updated 8 months ago

https://github.com/bc0de0/bash-scripts/blob/master/system_info.sh