Bash Scripting (.SH)

Linux shell scripting with development environments, constructs, and automation

What is Bash Scripting?

Bash (Bourne Again Shell) scripting allows you to automate tasks, combine commands, and create powerful tools using shell commands and programming constructs.

Key Features:
  • Automation: Automate repetitive tasks
  • System Integration: Interface with OS and applications
  • Text Processing: Parse and manipulate text data
  • Process Control: Manage system processes
  • File Operations: Batch file management
  • Network Operations: Network automation tasks
  • Data Collection: Gather system information
  • Monitoring: System health checks
Bash Script Structure:
#!/bin/bash
# Script description and metadata

# Global variables
SCRIPT_NAME="example"
VERSION="1.0"

# Function definitions
function main() {
    echo "Starting $SCRIPT_NAME v$VERSION"
    # Main script logic here
}

# Error handling
set -euo pipefail  # Exit on error, undefined vars, pipe failures
trap 'echo "Error on line $LINENO"' ERR

# Main execution
main "$@"

Shebang and Execute Permissions

Shebang (#!) - Script Interpreter Declaration
#!/bin/bashStandard bash interpreter
#!/usr/bin/env bashPortable bash (finds bash in PATH)
#!/bin/shPOSIX shell (most compatible)
Execute Permissions
# Make script executable
chmod +x script.sh

# Different permission levels
chmod 755 script.sh  # Owner: rwx, Group: r-x, Others: r-x
chmod 750 script.sh  # Owner: rwx, Group: r-x, Others: ---
chmod 700 script.sh  # Owner: rwx, Group: ---, Others: ---

# Execute script
./script.sh           # From current directory
/path/to/script.sh    # Full path
bash script.sh        # Explicitly with bash

🐚 Bash Quick Facts

  • Created: 1989 by Brian Fox
  • Default Shell: Most Linux distributions
  • File Extension: .sh (convention)
  • Latest Version: Bash 5.x
  • License: GNU GPL

Common Use Cases

System Administration

Server setup, user management, service control

Backup & Recovery

Automated backups, log rotation, cleanup

Monitoring

Health checks, performance metrics, alerting

DevOps

CI/CD pipelines, deployment automation