Founding offer: 50% off your first year

See plans →
Coding

Bash Script Prompt for Shell Automation

A structured prompt that writes a safe, portable Bash script for your task — with checks, comments, and a real example output.

Enhanced prompt — ready to use
ROLE You are a senior DevOps engineer with expertise in shell scripting. Your goal is to create a reliable and efficient Bash script to automate a specified task. TASK Write a Bash script to accomplish a clear, defined objective. Think step by step, considering potential edge cases and common pitfalls in shell scripting. Before finalizing, verify your script for correctness, robustness, and best practices. CONTEXT The user requests a Bash script, but no specific functionality is provided. Assume the script should serve a common automation purpose suitable for a general audience of system administrators or developers. REQUIREMENTS - Write a clear, concise, and well-commented Bash script. - Include error handling and input validation where applicable. - Follow shell scripting best practices for readability and performance. - Think through this step by step, including edge cases. - Before finalizing, verify your answer for correctness and completeness. - Assumptions: The script will run on a typical Unix-like environment with Bash available. No additional dependencies are specified. OUTPUT FORMAT Provide the complete Bash script in a code block, accompanied by brief explanatory comments inline and a short summary of its functionality.

or write yours rough and click once

That's what PromptAI produced from this vague prompt: write a bash script

Example result

```bash #!/bin/bash # # backup_dir.sh - Incremental backup script for a specified directory # # This script creates a timestamped compressed backup of a given source directory, # storing it in a specified backup directory. The script performs input validation, # checks for potential errors, and avoids common pitfalls. # # Usage: ./backup_dir.sh /path/to/source /path/to/backup # set -euo pipefail # Strict mode for safety # Function to print usage instructions usage() { echo "Usage: $0 SOURCE_DIRECTORY BACKUP_DIRECTORY" echo "Creates a compressed backup of SOURCE_DIRECTORY inside BACKUP_DIRECTORY." exit 1 } # Function to print errors to stderr error() { echo "Error: $1" >&2 exit 1 } # Validate arguments if [[ $# -ne 2 ]]; then usage fi SOURCE_DIR=$1 BACKUP_DIR=$2 # Resolve absolute paths for clarity and safety SOURCE_DIR=$(readlink -f -- "$SOURCE_DIR") || error "Failed to resolve source directory path." BACKUP_DIR=$(readlink -f -- "$BACKUP_DIR") || error "Failed to resolve backup directory path." # Check that source directory exists and is a directory if [[ ! -d "$SOURCE_DIR" ]]; then error "Source directory '$SOURCE_DIR' does not exist or is not a directory." fi # Check that backup directory exists; if not, attempt to create it if [[ ! -d "$BACKUP_DIR" ]]; then echo "Backup directory '$BACKUP_DIR' does not exist. Attempting to create it..." if ! mkdir -p -- "$BACKUP_DIR"; then error "Failed to create backup directory '$BACKUP_DIR'. Check permissions." fi fi # Check write permission on backup directory if [[ ! -w "$BACKUP_DIR" ]]; then error "No write permission on backup directory '$BACKUP_DIR'." fi # Generate a timestamped backup filename TIMESTAMP=$(date +%Y%m%d_%H%M%S) SRC_BASENAME=$(basename -- "$SOURCE_DIR") BACKUP_FILE="${BACKUP_DIR}/${SRC_BASENAME}_backup_${TIMESTAMP}.tar.gz" # Inform user about the backup operation echo "Backing up '$SOURCE_DIR' to '$BACKUP_FILE' ..." # Create the compressed tar archive if tar -czf "$BACKUP_FILE" -C "$(dirname "$SOURCE_DIR")" "$SRC_BASENAME"; then echo "Backup successful." else error "Backup failed during archive creation." fi # Optional: Verify the archive integrity (basic test) if tar -tzf "$BACKUP_FILE" &> /dev/null; then echo "Backup archive verified successfully." else error "Backup archive verification failed." fi exit 0 ``` --- ### Summary This Bash script automates the task of backing up a directory by creating a compressed (tar.gz) archive with a timestamped filename. It accepts two positional arguments: the source directory to back up and the target backup directory. The script validates arguments, ensures directories exist (creating the backup directory if needed), checks permissions, and handles errors gracefully. It also verifies the integrity of the created backup archive before completing. This makes it a safe and reliable tool for sysadmins and developers to perform incremental backups quickly and consistently.

How to adapt it

  • Tell it the OS and what should happen on failure for a robust script.
  • Ask it to use 'set -euo pipefail' and quote variables for safety.

Have your own rough prompt? Enhance it in one click.

Enhance your own prompt

Or use the ChatGPT prompt enhancer right inside ChatGPT, the AI prompt enhancer for every other tool, or the prompt enhancer for Claude Code and Cursor in your editor.

More coding prompts