Skip to content
/ bash-lib Public

A powerful Bash library with colored output, logging, validation, and system utilities. Install with one curl command and start building better shell scripts immediately.

License

Notifications You must be signed in to change notification settings

mrvi0/bash-lib

Repository files navigation

Bash Library

A reusable bash functions and utilities library for development, deployment, and system management.

🌍 Language Selection

📋 Table of Contents

⬆️ Back to top

🚀 Quick Start

Simple Connection (No Installation)

The easiest way - connect the library directly in your script with one line:

#!/usr/bin/env bash
# Connect the entire library with one line
source <(curl -fsSL https://raw.githubusercontent.com/mrvi0/bash-lib/main/bash-lib-standalone.sh)

# Now you can use all functions
logging::info "Script started"
colors::success "Operation completed successfully!"

if validation::is_email "[email protected]"; then
    echo "Email is valid"
fi

⬆️ Back to top

Universal Method (Recommended)

For more efficient usage, create a loading function:

#!/usr/bin/env bash

# Universal bash-lib loading
load_bash_lib() {
    local remote_url="https://raw.githubusercontent.com/mrvi0/bash-lib/main/bash-lib-standalone.sh"
    local cache_dir="$HOME/.cache/bash-lib"
    local cache_file="$cache_dir/bash-lib-standalone.sh"
    
    # Check if library is already loaded
    if [[ -n "$__BASH_LIB_IMPORTED" ]]; then
        return 0
    fi
    
    # Create cache directory
    mkdir -p "$cache_dir"
    
    # Use cache if not expired (24 hours)
    if [[ -f "$cache_file" ]] && [[ $(($(date +%s) - $(stat -c %Y "$cache_file"))) -lt 86400 ]]; then
        source "$cache_file"
    else
        # Download and cache
        if curl -fsSL "$remote_url" -o "$cache_file"; then
            source "$cache_file"
        else
            echo "Error loading library"
            exit 1
        fi
    fi
}

# Load library
load_bash_lib

# Use functions
logging::info "Script started"
colors::success "Ready to work!"

⬆️ Back to top

Installation

# Clone repository
git clone https://github.com/mrvi0/bash-lib.git
cd bash-lib

# Install library
sudo ./scripts/install.sh

# Activate in current session
source ~/.bashrc

⬆️ Back to top

Usage

#!/usr/bin/env bash
# Connect library
source /usr/local/lib/bash-lib/bash-lib.sh

# Use functions
logging::info "Starting work"
colors::success "Operation completed successfully"

if validation::is_email "[email protected]"; then
    echo "Valid email"
fi

⬆️ Back to top

📦 Versioning

Each stable version of the library is marked with a git tag.

How to connect a specific version:

source <(curl -fsSL https://raw.githubusercontent.com/mrvi0/bash-lib/v1.0.0/bash-lib-standalone.sh)

How to check current version:

bash_lib::version

How to check version in script:

if [[ "$__BASH_LIB_VERSION" != "1.0.0" ]]; then
    echo "Error: bash-lib version 1.0.0 required"
    exit 1
fi

See all versions on the Releases tab or by git tags.

⬆️ Back to top

📁 Project Structure

bash-lib/
├── src/                          # Main library code
│   ├── core/                     # Basic functions
│   │   ├── colors.sh            # Colored output
│   │   ├── logging.sh           # Logging
│   │   └── validation.sh        # Data validation
│   ├── io/                      # File and I/O operations
│   ├── system/                  # System functions
│   ├── development/             # Development functions
│   └── database/                # Database operations
├── examples/                     # Usage examples
│   ├── basic_usage.sh          # Basic example
│   ├── simple_usage.sh         # Simple connection
│   ├── cached_usage.sh         # With caching
│   ├── local_usage.sh          # Local file
│   └── universal_usage.sh      # Universal method
├── tests/                        # Tests
├── docs/                         # Documentation
├── scripts/                      # Installation scripts
├── bash-lib.sh                  # Main import file
├── bash-lib-standalone.sh       # Single file for simple connection
└── README.md

⬆️ Back to top

🎯 Core Modules

Core (Basic Functions)

Colors (src/core/colors.sh)

Colored terminal output with support for various colors and styles.

colors::success "Successful operation"
colors::error "Error"
colors::warning "Warning"
colors::info "Information"
colors::debug "Debug information"
colors::highlight "Highlighted text"

Logging (src/core/logging.sh)

Logging system with various levels and formats.

# Set logging level
logging::set_level debug

# Logging
logging::info "Information message"
logging::debug "Debug information"
logging::warn "Warning"
logging::error "Error"
logging::fatal "Critical error (with exit)"

# Logging to file
logging::set_file "/var/log/myapp.log"

Validation (src/core/validation.sh)

Validation of various data types.

# Email validation
validation::is_email "[email protected]"

# Integer validation
validation::is_integer "123"
validation::is_positive_integer "456"

# Range validation
validation::is_in_range "50" "1" "100"

# Port validation
validation::is_port "8080"

# File existence check
validation::file_exists "/path/to/file"

# Combined validation
validation::all "[email protected]" validation::is_email validation::is_not_empty

⬆️ Back to top

📚 Usage Examples

Basic Example

#!/usr/bin/env bash
source <(curl -fsSL https://raw.githubusercontent.com/mrvi0/bash-lib/main/bash-lib-standalone.sh)

# Setup logging
logging::set_level info
logging::set_file "/tmp/myapp.log"

# Main logic
logging::info "Starting application"

# Input validation
if ! validation::is_email "$1"; then
    colors::error "Invalid email: $1"
    logging::error "Email validation failed"
    exit 1
fi

colors::success "Email is valid: $1"
logging::info "Application completed successfully"

⬆️ Back to top

Progress Bar Example

#!/usr/bin/env bash
source <(curl -fsSL https://raw.githubusercontent.com/mrvi0/bash-lib/main/bash-lib-standalone.sh)

echo "Processing files..."
for i in {1..100}; do
    colors::progress_bar "$i" "100" "30" "Processing"
    sleep 0.01
done
echo "Done!"

⬆️ Back to top

🔧 Installation Methods

1. Direct Download (Simplest)

source <(curl -fsSL https://raw.githubusercontent.com/mrvi0/bash-lib/main/bash-lib-standalone.sh)

Pros: Very simple, always up-to-date version Cons: Requires internet on each run, slower

2. Caching (Recommended)

load_bash_lib() {
    local cache_dir="$HOME/.cache/bash-lib"
    local cache_file="$cache_dir/bash-lib-standalone.sh"
    local remote_url="https://raw.githubusercontent.com/mrvi0/bash-lib/main/bash-lib-standalone.sh"
    
    mkdir -p "$cache_dir"
    if [[ -f "$cache_file" ]] && [[ $(($(date +%s) - $(stat -c %Y "$cache_file"))) -lt 86400 ]]; then
        source "$cache_file"
    else
        curl -fsSL "$remote_url" -o "$cache_file" && source "$cache_file"
    fi
}
load_bash_lib

Pros: Fast, 24-hour caching, works without internet Cons: Slightly more complex

3. Local File

# Download file once
curl -fsSL https://raw.githubusercontent.com/mrvi0/bash-lib/main/bash-lib-standalone.sh -o bash-lib-standalone.sh

# Use in scripts
source "./bash-lib-standalone.sh"

Pros: Maximum speed, works without internet Cons: Need to update manually

4. Universal Method

See examples/universal_usage.sh - combines all methods with priority.

⬆️ Back to top

🎯 Recommendations

Scenario Recommended Method Reason
Quick Scripts Direct Download Maximum simplicity
Serious Projects Caching Balance of speed and relevance
Production Local File Reliability and speed
Development Universal Flexibility

⬆️ Back to top

🔧 Installation and Setup

Automatic Installation

# Install in standard directories
sudo ./scripts/install.sh

# Install in user directories
./scripts/install.sh -d ~/.local/lib/bash-lib -b ~/.local/bin

Manual Installation

# Copy files
sudo cp -r src /usr/local/lib/bash-lib/
sudo cp bash-lib.sh /usr/local/lib/bash-lib/

# Add to .bashrc
echo 'source /usr/local/lib/bash-lib/bash-lib.sh' >> ~/.bashrc
source ~/.bashrc

⬆️ Back to top

🧪 Testing

# Run examples
./examples/basic_usage.sh
./examples/simple_usage.sh
./examples/cached_usage.sh
./examples/local_usage.sh
./examples/universal_usage.sh

# Check function availability
bash-lib --help

⬆️ Back to top

📖 Documentation

⬆️ Back to top

🤝 Contributing

  1. Fork the repository
  2. Create a branch for your feature (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

⬆️ Back to top

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

⬆️ Back to top

🆘 Support

If you have questions or issues:

  1. Check the documentation
  2. Look at the examples
  3. Create an Issue

⬆️ Back to top

🔄 Versions

  • v1.0.0 - First stable release with full functionality
    • Colored output and formatting
    • Logging system with levels
    • Data validation (email, numbers, files, ports)
    • System utilities (internet check, system information)
    • Universal functions (confirmations, headers, progress bars)
    • Compatibility with old functions
  • v1.1.0 - Planned: IO modules, extended system functions
  • v1.2.0 - Planned: Development modules, database modules

⬆️ Back to top

About

A powerful Bash library with colored output, logging, validation, and system utilities. Install with one curl command and start building better shell scripts immediately.

Topics

Resources

License

Stars

Watchers

Forks

Languages