Motivation

Have a set of my own bash functions which can be used in terminal and scripts

Requirments on the syste

  • Names of functions can be displayed in terminal
  • Each function has a help and short description
  • Functions are all in one file/one directory, so it can be backed-up, restored
  • Work for scripting and in terminal

Use cases

  • making image panels
  • processing videos
  • processing audio
  • parsing simple config files
  • cleaning after itself
  • creating a directory with a date for a script

Initial design as suggested by chatGPT

Where to put the functions

Files

File with functions:

#=== bf_mybackup ========================================================
# Backup a directory to a timestamped archive
# Usage: mybackup <directory>
#=====================================================================
bf_mybackup() {
    ...
}

#=== bf_mkgit ===========================================================
# Initialize a Git repo, create main, make initial commit
# Usage: mkgit <directory>
#=====================================================================
bf_mkgit() {
    ...
}

Function to display functions:

help_bf() {
    local func="$1"
    local file="$HOME/.bash_functions"

    if [ -z "$func" ]; then
        echo "Available functions:"
        grep '^#=== ' "$file" | sed 's/#=== //'
        return
    fi

    awk -v f="$func" '
        /^#===/ {name=$2}
        name==f {print}
        END { if (!name) print "Function not found." }
    ' "$file"
}

Usage

help_bf          # displays all functions
help_bf function # displays help of function