🆕 The new HTB Certified Offensive AI Expert (HTB COAE) is officially here! Learn More

Introduction to Bash Scripting (LetsDefend)

mini-module tag Mini-Module

This module is designed to provide detection engineers in the cybersecurity field with a foundational understanding of Bash scripting, enabling them to bolster their capabilities in threat detection and incident response.

Created by letsdefendio

Medium General

Summary

The Introduction to Bash Scripting module is designed for cybersecurity detection engineers seeking to enhance their scripting skills and Security professionals looking to automate and streamline threat detection processes. Throughout this module, students will develop proficiency in Bash scripting to streamline cybersecurity detection tasks and enhanced abilities in automating security processes and incident response.

Recommended prior knowledge:

  • Basic knowledge of Linux/Unix operating systems

Introduction to Bash


History Of Bash

The Unix shell serves as an interface that allows users to interact with operating systems. This shell receives the user's commands and passes those commands to the operating system. Shell is both a scripting language and a program capable of interpreting commands.

The first Unix shell was developed by Stephen R. Bourne, one of the original creators of Unix, in 1979, and was therefore named Bourne Shell. The Bourne shell (sh) became the most widely used shell on Unix and Unix-like systems.

In the mid-1980s, Brian Fox developed Bash (Bourne Again Shell) for the GNU project with the aim of replacing Bourne Shell. Bash is a shell that encompasses the features of Bourne Shell but also provides more advanced features and a user-friendly interface. Bash is used as the standard shell for many Unix-like systems, including Linux and macOS.

The development of Bash continued with the fixing of various bugs as well as adding new features such as command line editing, shell job control, shell functions, and many more.

We can say that Bash is the tool of choice by many system administrators and developers, as it proves the importance and flexibility of the shell and also makes it easy to write scripts. This has made Bash frequently used, especially in system administration and automated tasks.


Cons and Pros

Because Bash was created in the UNIX systems, it has the inherent robustness and problem-solving abilities of that environment. Let's take a look at the strengths that characterize Bash:

Works Anywhere

You can find Bash almost anywhere. It works on Linux, Mac, and even Windows (thanks to the Windows Subsystem for Linux). In fact, there is a BASH in your car and on your TV at home, even if you don't see it. This means that the scripts you write will work on almost any platform.

Automation Capabilities

Bash is the number one method to automate tasks. It is a powerful tool which you can get repetitive tasks done. Using Bash's capabilities is a great way to save time and reduce errors.

Productivity

Thanks to Bash, you have the ability to combine complex commands and perform many tasks at once. This helps you to be more productive.

Integration Capability with Many Different Tools

Bash integrates with many Unix commands and tools perfectly. This allows you to perform more complex operations and use many different tools in the same script.

We've seen Bash's strengths above. Unfortunately, it also has some weaknesses. Before listing them, let's point out that Bash is actually excellent as a system management tool, its disadvantages appear when compared to other modern scripting languages:

Complexity

Bash can be a bit complex, especially for beginners. Its syntax can be a bit odd and difficult to remember from time to time.

Debugging: Debugging Bash can be a bit of a hassle. Bash usually does not display the line number with error, and so this makes it difficult to find errors.

Performance

Bash is a bit slower in terms of performance compared to other languages. While this is not usually a problem, it may be from time to time when working with very large datasets or when you need to perform tasks very quickly.

Advanced Features

Bash does not have some advanced features that languages like Python or JavaScript offer. For instance, Bash lacks features like first-class functions, data structures, etc.

To summarize, Bash is a great option in many cases, especially for tasks related to system administration and automation. However, other languages may be more suitable for certain situations and requirements.


Terminology

In this section, let's explain some very basic concepts for those unfamiliar with the Unix world. These will make the later stages of the module more understandable.

Shell

Shell allows users to communicate with the operating system. Shell is a type of user interface and is usually text-based. Shells also feature a scripting language and allow users to save commands as a written script and run this script automatically later. Bash is one of the most popular Unix shells and often comes as the default shell on Linux and macOS.

Command Line (CLI)

The command line is an interface where users enter commands into the shell. It is usually accessed via a terminal or console application. Users tell the operating system what to do by typing text-based commands and pressing “Enter”.

Command

A command is an instruction given to the operating system to perform a certain action. For example, ls is a Unix command that lists files and directories in the current directory to the user. cd is another command and it changes the current working directory.

Script

A script is an automated set of commands. These commands are stored in a file and then interpreted and executed by the shell. Bash scripts are usually stored in files ending with the .sh extension and can contain a number of command and control structures (if-else constructs, loops, etc.).

Pipelining

Pipelining is the ability to use the output of one command as the input of another command. This is done with the | symbol. For example, the ls | grep .txt command passes the output of the ls command to the grep command and lists only files with the .txt extension.

Environment Variables

Environment variables allow the shell and the programs it runs to store and access certain values. For example, the PATH environment variable specifies which directories the shell will check for commands. These environment variables can be set system-wide or for a specific user.

STDIN, STDOUT ve STDERR

These terms refer to standard channels for the input and output of the shell and programs. STDIN usually represents input from the keyboard, STDOUT represents correct output and STDERR represents erroneous output.

Execution Permissions

On Unix-based systems, it determines whether a file or script is executable. These permissions can be changed with the chmod command.

These terms are some of the basic concepts you will encounter in the Bash and general Unix world. Understanding them is an essential part of working effectively on a Unix-based system and writing Bash scripts.


Hello World

Let's write a little bash script together:

First, open a text editor. On Linux it can usually be nano, vi or emacs. On Windows you can use Notepad or another text editor. Basically, what we're going to do is create a text file with .sh extension

Type the following two lines in the text editor:

#!/bin/bash
echo "Hello World"

The #!/bin/bash line indicates that this script should be run in Bash.

The echo command, on the other hand, prints text to the screen.

Save this file as hello_word.sh .

Now we have to make sure the script is executable. We can do this by running the following command in terminal:

chmod +x hello_word.sh

This command adds executable permissions to hello_world.sh.

Finally, we can run our script:

./hello_word.sh

What do you see on the screen?

We've made a basic example of creating and running a simple "Hello Word" script in Bash. This script is a great starting point for demonstrating the simplicity and ease of use of the Bash scripting language.

Sign Up / Log In to Unlock the Module

Please Sign Up or Log In to unlock the module and access the rest of the sections.

Relevant Paths

This module progresses you towards the following Paths

Programming for Cybersecurity

Gain essential coding skills for cybersecurity, across PowerShell, Bash, Python, and Go, to protect against digital threats and enhance your abilities in the ever-evolving field of cyber defense.

Hard Path Sections 63 Sections
Required: 410
Reward: +90
Path Modules
Medium
Path Sections 10 Sections
Reward: +10 NEW
The "Introduction to Python" module is designed to familiarize detection engineers in cybersecurity with the Python programming language, enabling them to effectively apply it in the context of threat detection and analysis.
Python for Blue Team
mini module tag Mini-Module
Hard
Path Sections 8 Sections
Reward: +10 NEW
The "Python for Blue Team" module focuses on equipping cybersecurity professionals within the Blue Team with practical Python skills tailored for effective threat detection and strategic defense methodologies.
Medium
Path Sections 7 Sections
Reward: +10 NEW
This module is designed to provide detection engineers in the cybersecurity field with a foundational understanding of Bash scripting, enabling them to bolster their capabilities in threat detection and incident response.
Hard
Path Sections 6 Sections
Reward: +10 NEW
The "Advanced Bash Scripting" module is designed to improve participants' knowledge and skills in using complex scripting techniques for cybersecurity. It aims to enable individuals to effectively tackle intricate security challenges by employing advanced scripting methodologies.
Hard
Path Sections 7 Sections
Reward: +10 NEW
The "Bash Scripting for Blue Team" module focuses on providing the essential scripting skills required for cybersecurity defense, specifically tailored for the Blue Team. It aims to equip individuals with the practical knowledge needed to proactively manage and defend against security threats.
Easy
Path Sections 7 Sections
Reward: +10 NEW
The "Introduction to PowerShell" module is designed to provide participants with a fundamental understanding of PowerShell, covering installation, basic command usage, file and directory operations, optimization techniques, and effective error handling for a well-rounded learning experience.
Hard
Path Sections 6 Sections
Reward: +10 NEW
The "PowerShell for Analysis" module is designed to equip cybersecurity professionals with the skills to leverage PowerShell's scripting capabilities for thorough and effective analysis of data, enabling robust threat investigations within cybersecurity.
Hard
Path Sections 7 Sections
Reward: +10 NEW
The "Advanced PowerShell for Blue Team" module is designed to improve participants' knowledge of using advanced PowerShell techniques. This module is specifically customized to enhance cybersecurity defense strategies for the Blue Team.
Go for Cybersecurity
mini module tag Mini-Module
Medium
Path Sections 5 Sections
Reward: +10 NEW
Discover how to detect and thwart malicious activity across various domains with our specialized Golang for Cybersecurity module. Gain the expertise to analyze, identify, and counter potential threats lurking within emails, networks, systems, and files. Through practical exercises and real-world examples, learners will master the application of Golang to fortify their cybersecurity defenses and safeguard critical digital assets.

Google Cybersecurity Certificate Preparation

The Google Cybersecurity Certificate Preparation Path equips learners with the foundational knowledge and practical skills needed to succeed in the Google Cybersecurity Certificate and to build a credible footing for an entry-level cybersecurity role. The path moves from core security principles into the hands-on competencies that employers expect, covering cybersecurity best practices, scripting and Linux fundamentals, threat and vulnerability management, and the essentials of working with a SIEM.

Medium Path Sections 224 Sections
Required: 1120
Reward: +240
Path Modules
SOC Fundamentals
mini module tag Mini-Module
Fundamental
Path Sections 10 Sections
Reward: +10
This module provides comprehensive coverage of SOC Fundamentals concepts and techniques. Topics covered include Common Mistakes made by SOC Analysts, EDR - Endpoint Detection and Response, Introduction to SOC, Log Management, SIEM and Analyst Relationship, and more.
Network Log Analysis
mini module tag Mini-Module
Hard
Path Sections 10 Sections
Reward: +10
This module provides comprehensive coverage of Network Log Analysis concepts and techniques. Topics covered include DNS Log Analysis, Firewall Log Analysis, Generic Log Analysis (Netflow), IDSIPS Log Analysis, Introduction to Network Log Analysis, and more.
Easy
Path Sections 6 Sections
Reward: +10 NEW
Delve into the art of network packet analysis in this comprehensive module. Gain essential skills to diagnose network issues, detect security threats, and optimize network performance. Learn protocols, tools, and techniques to capture, analyze, and interpret network traffic. Uncover hidden insights from packet headers and payloads, troubleshoot latency and congestion, and enhance network security. Whether you're a network administrator, security analyst, or IT professional, this module equips you with the knowledge and hands-on experience to excel in network packet analysis.
Medium
Path Sections 7 Sections
Reward: +10 NEW
This module is designed to provide detection engineers in the cybersecurity field with a foundational understanding of Bash scripting, enabling them to bolster their capabilities in threat detection and incident response.
Hard
Path Sections 7 Sections
Reward: +10 NEW
The "Bash Scripting for Blue Team" module focuses on providing the essential scripting skills required for cybersecurity defense, specifically tailored for the Blue Team. It aims to equip individuals with the practical knowledge needed to proactively manage and defend against security threats.
Hard
Path Sections 7 Sections
Reward: +10 NEW
The "Vulnerability Management" module is a comprehensive training program focusing on identifying, assessing, and managing system vulnerabilities within the realm of cybersecurity. It offers in-depth insights into various vulnerability management strategies, techniques, and tools essential for building robust defense systems. Participants will gain practical knowledge to proactively identify, prioritize, and mitigate vulnerabilities, minimizing potential risks and fortifying cybersecurity measures effectively within organizational infrastructures. The module aims to empower cybersecurity professionals and IT personnel with the skills to protect systems against potential threats and enhance overall cybersecurity posture.
Medium
Path Sections 13 Sections
Reward: +10
This module provides comprehensive coverage of Introduction to Cryptology concepts and techniques. Topics covered include Base64 EncodingDecoding, Basic Concepts of Cryptology, Cryptographic Attacks, Digital Signatures, Hash Functions, and more.
Medium
Path Sections 13 Sections
Reward: +10
Elevate your cybersecurity capabilities with our Incident Response on Linux module. This program provides thorough training on detecting, analyzing, and mitigating security threats in Linux environments. Tailored for IT professionals, system administrators, incident responders and security analysts, the module offers practical, hands-on experience to strengthen your incident response skills. By mastering essential tools and techniques, you’ll be equipped to safeguard networks and protect data effectively.
Medium
Path Sections 13 Sections
Reward: +10
Enhance your cybersecurity expertise with our Incident Response on Windows module. Dive into essential techniques for detecting, analyzing, and mitigating security threats on Windows systems. Gain hands-on experience and practical knowledge to handle incidents swiftly and efficiently.
Medium
Path Sections 10 Sections
Reward: +10 NEW
The "Introduction to Python" module is designed to familiarize detection engineers in cybersecurity with the Python programming language, enabling them to effectively apply it in the context of threat detection and analysis.
Python for Blue Team
mini module tag Mini-Module
Hard
Path Sections 8 Sections
Reward: +10 NEW
The "Python for Blue Team" module focuses on equipping cybersecurity professionals within the Blue Team with practical Python skills tailored for effective threat detection and strategic defense methodologies.
Network Security
mini module tag Mini-Module
Medium
Path Sections 10 Sections
Reward: +10 NEW
During this module section, we'll explore the intricacies of network security, focusing on practical security strategies and techniques to enhance your ability to protect digital systems effectively.
Medium
Path Sections 11 Sections
Reward: +10 NEW
"Network Design and Security Products" is a comprehensive module focused on the intricate world of network architecture and the critical role security products play within it. This module delves into the strategic planning and implementation of robust network infrastructures, with an emphasis on security considerations. Students will gain insights into the fundamentals of network design, including network topologies, protocols, and scalability. They will also explore the latest advancements in security products, such as firewalls, intrusion detection systems, web application firewalls (WAF), and encryption technologies. Throughout the module, students will learn to assess potential network vulnerabilities and develop strategies to mitigate risks.
Network Fundamentals
mini module tag Mini-Module
Fundamental
Path Sections 10 Sections
Reward: +10 NEW
This module provides comprehensive coverage of Network Fundamentals concepts and techniques. Topics covered include About Network Fundamentals Module, IP Addressing Mechanism, Introduction to Network Fundamentals, Network Address Translation (NAT), Network Devices, and more.
Easy
Path Sections 12 Sections
Reward: +10 NEW
This module provides comprehensive coverage of Network Fundamentals II concepts and techniques. Topics covered include Address Resolution Protocol (ARP), Basic Concepts, Internet Control Message Protocol (ICMP), Internet Protocol (IP), Introduction to Network Fundamentals 2, and more.
Network Protocols
mini module tag Mini-Module
Easy
Path Sections 8 Sections
Reward: +10 NEW
This module provides comprehensive coverage of Network Protocols concepts and techniques. Topics covered include Domain Name System (DNS) - 1, Domain Name System (DNS) - 2, Introduction to Network Protocols, Telnet (Teletype Network) Protocol, Transmission Control Protocol (TCP) - 1, and more.
Network Protocols - 2
mini module tag Mini-Module
Easy
Path Sections 8 Sections
Reward: +10 NEW
This module will teach you how application-level protocols work and how you can use it as SOC Analyst or Incident Responder. As an investigator, you need to know the basics of these protocols to use it as evidence.
SIEM 101
mini module tag Mini-Module
Fundamental
Path Sections 6 Sections
Reward: +10 NEW
This module provides comprehensive coverage of SIEM 101 concepts and techniques. Topics covered include Alerting, Log Aggregation and Parsing, Log Collection, Log Storage, SIEM Introduction.
Security Solutions
mini module tag Mini-Module
Easy
Path Sections 14 Sections
Reward: +10
This module provides comprehensive coverage of Security Solutions concepts and techniques. Topics covered include Antivirus Software (AV), Asset Management Solutions, Data Loss Prevention (DLP), Email Security Solutions, Endpoint Detection and Response (EDR), and more.
Medium
Path Sections 6 Sections
Reward: +10
In this module, we will cover how to handle cybersecurity incidents properly, incident response processes with its proper order along with the recommendations of the "Computer Security Incident Handling Guide".
Easy
Path Sections 6 Sections
Reward: +10 NEW
The "Security Audit and Testing" module offers a comprehensive understanding of assessing and enhancing digital security through rigorous auditing and testing procedures. Participants delve into techniques for identifying vulnerabilities, conducting risk assessments, and employing industry-standard tools to evaluate the resilience of networks, applications, and databases. Through practical applications and theoretical knowledge, students learn to interpret audit findings, prioritize security measures, and develop actionable strategies to mitigate risks effectively. This module equips learners with essential skills to safeguard sensitive information and maintain organizational integrity in today's interconnected digital environment.
Linux for Blue Team
mini module tag Mini-Module
Fundamental
Path Sections 14 Sections
Reward: +10 NEW
This module provides comprehensive coverage of Linux for Blue Team concepts and techniques. Topics covered include About This Course, Archive File Formats, Basic Terminal Commands - 1, Basic Terminal Commands - 2, Introduction to Linux, and more.
Secure Network Design
mini module tag Mini-Module
Medium
Path Sections 9 Sections
Reward: +10 NEW
The "Secure Network Design" module offers comprehensive insights into designing and implementing networks that prioritize security, equipping cyber people with the expertise to create resilient and protected digital infrastructures against evolving cyber threats. Through a blend of theoretical concepts and practical applications, participants will gain a deep understanding of network architecture, access controls, and risk management strategies, enabling them to develop and maintain secure communication channels within a dynamic and interconnected technological environment.
Job Hunting
mini module tag Mini-Module
Medium
Path Sections 6 Sections
Reward: +10
This module provides comprehensive coverage of Job Hunting concepts and techniques. Topics covered include Getting Ready for the Interview, How and Where to Look for a Job, How to Prepare a Cybersecurity Resume, Introduction to Job Hunting, What Should You Expect, and more.