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
  • Bash Scripting Basics Guide
  • 4. Basic Syntax of Bash Scripts
  • 5. Sample Script: System Information and Monitoring Tool
  1. Bash Scripting

Fundamentals of Bash


Bash Scripting Basics Guide

1. Introduction to Shell and Bash

A shell is an interface that allows users to interact with the operating system. It interprets the commands you enter and executes them.

  • Bash (Bourne Again Shell) is the most commonly used shell on Linux systems and is widely used for scripting, automating tasks, and managing system processes.

  • Bash is a command processor that typically runs in a text window where users type commands that cause actions. It's the default shell on most Linux distributions.

2. Other Common Shells

There are multiple types of shells in Linux, and here are a few common ones:

  • Bash (/bin/bash): Default shell for many Linux distributions.

  • Zsh (/bin/zsh): An extended version of Bash, with more features.

  • Ksh (/bin/ksh): Korn shell, known for its advanced scripting capabilities.

  • Fish (/usr/bin/fish): A user-friendly shell with a focus on interactive features.

3. Checking and Changing the Current Shell

To see which shell you're currently using:

echo $SHELL

To change your shell temporarily:

bash     # or zsh, ksh, etc.

To permanently change the default shell, you can use:

chsh -s /bin/zsh

This command changes your default shell, but requires you to log out and log back in for the changes to take effect.


4. Basic Syntax of Bash Scripts

Before diving into an exercise, let’s quickly cover some of the essential components of a Bash script:

  • Shebang (#!): Tells the system what interpreter to use to execute the script. Most scripts will use #!/bin/bash.

  • Variables: Used to store and manipulate data.

  • Conditionals: if, else, elif used for decision-making.

  • Loops: for, while, and until used to repeat actions.

  • Input/Output Redirection: Redirecting input/output with operators (>, >>, |).

Basic Script Structure

#!/bin/bash
# This is a comment
# Script code goes here

5. Sample Script: System Information and Monitoring Tool

Exercise Objective:

This script will:

  1. Create a directory.

  2. Move a file into the directory.

  3. Copy the file back out of the directory.

  4. Display the file listing at each step.

  5. Remove the directory and file (as a cleanup step).


6. How to Run the Script:

  1. Open a terminal and create a new script file:

    nano bash_basics.sh
  2. Paste the sample script into the file and save it.

  3. Make the script executable:

    chmod +x bash_basics.sh
  4. Run the script:

    ./bash_basics.sh
PreviousCommon Network ToolsNextVariables, Branching and Loops

Last updated 8 months ago

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