Skip to content

Instantly share code, notes, and snippets.

@chriskyfung
Created March 27, 2025 08:42
Show Gist options
  • Save chriskyfung/5a6b5bfc1c516e5e3fdb74a5a6047e9d to your computer and use it in GitHub Desktop.
Save chriskyfung/5a6b5bfc1c516e5e3fdb74a5a6047e9d to your computer and use it in GitHub Desktop.
A Bash script to display shell history with flexible timestamp formats, timezone support, and custom options for enhanced usability.
#!/bin/bash
# Enhanced History Viewer v1.0 - Flexible Timestamp Formats
# Generated by Qwen2.5-Max
# Get the script name correctly for both sourced and executed contexts
SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
# Function to display help message
show_help() {
echo "Usage: source $SCRIPT_NAME [OPTIONS]"
echo
echo "Display shell history with flexible timestamp formatting"
echo
echo "Options:"
echo " --iso ISO 8601 format with timezone (default)"
echo " --utc ISO 8601 in UTC (e.g., 2024-03-20T14:30:00Z)"
echo " --local Local time without timezone (e.g., 2024-03-20 14:30:00)"
echo " --format FMT Custom strftime format (e.g., \"%Y-%m-%d %I:%M %p\")"
echo " --tz TZ Set timezone (e.g., \"America/New_York\")"
echo " --help Show this help message"
echo
echo "Examples:"
echo " source $SCRIPT_NAME --utc"
echo " source $SCRIPT_NAME --tz Europe/London --format \"%d/%m/%Y %H:%M\""
}
# Check if script is sourced or executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Script is executed directly, not sourced
case "$1" in
--help)
show_help
exit 0
;;
*)
echo "Error: This script must be sourced to access shell history." >&2
echo "Usage: source $SCRIPT_NAME [options]" >&2
exit 1
;;
esac
fi
# Save original settings
ORIGINAL_HISTTIMEFORMAT="$HISTTIMEFORMAT"
ORIGINAL_TZ="${TZ:-}"
# Configuration
FORMAT="%Y-%m-%dT%H:%M:%S%z" # Default ISO 8601 with timezone
TZ_SETTING=""
# Option parsing
while [[ $# -gt 0 ]]; do
case "$1" in
--iso)
FORMAT="%Y-%m-%dT%H:%M:%S%z"
TZ_SETTING=""
shift
;;
--utc)
FORMAT="%Y-%m-%dT%H:%M:%SZ"
TZ_SETTING="UTC"
shift
;;
--local)
FORMAT="%Y-%m-%d %H:%M:%S"
TZ_SETTING=""
shift
;;
--format)
if [[ -z "$2" ]]; then
echo "Error: --format requires a strftime format string" >&2
show_help
return 1
fi
FORMAT="$2"
shift 2
;;
--tz)
if [[ -z "$2" ]]; then
echo "Error: --tz requires a timezone identifier" >&2
show_help
return 1
fi
TZ_SETTING="$2"
shift 2
;;
--help)
show_help
return 0
;;
*)
echo "Error: Unknown option '$1'" >&2
echo "Valid options are: --iso, --utc, --local, --format, --tz, --help" >&2
return 1
;;
esac
done
# Apply settings
export TZ="${TZ_SETTING:-$ORIGINAL_TZ}"
export HISTTIMEFORMAT="${FORMAT} "
# Show history with temporary format
history
# Restore original settings
export HISTTIMEFORMAT="$ORIGINAL_HISTTIMEFORMAT"
export TZ="$ORIGINAL_TZ"
@chriskyfung
Copy link
Author

history.sh v1.0 - Enhanced Shell History Viewer with Flexible Timestamp Formatting

This Bash script enhances the standard shell history command by providing flexible timestamp formatting options, making it easier to analyze and interpret your command history. It supports various timestamp formats, including ISO 8601, UTC, local time, and custom strftime formats, while also allowing you to specify timezones for consistent output.

Key Features:

  • Flexible Timestamp Formats: Choose from predefined formats like ISO 8601 (--iso), UTC (--utc), or local time (--local), or define your own custom format using --format.
  • Timezone Support: Set a specific timezone (e.g., America/New_York) using the --tz option to ensure timestamps are displayed in your desired region.
  • Non-Destructive Operation: Temporarily modifies the HISTTIMEFORMAT and TZ environment variables without affecting your shell's persistent configuration.
  • Helpful Documentation: Displays detailed usage instructions and examples when using the --help option, ensuring ease of use for both sourced and direct execution.
  • Error Handling: Provides clear error messages and ensures the script is sourced correctly to access shell history.
  • AI-Generated: Created with assistance from Qwen2.5-Max for robustness and flexibility.

Usage Examples:

source history.sh --utc                # Show history with UTC timestamps
source history.sh --tz Europe/London   # Display timestamps in London time
source history.sh --format "%H:%M"     # Custom time-only format

Perfect for developers, system administrators, and power users who need precise control over how their shell history is displayed. Whether you're debugging commands, analyzing workflows, or simply organizing your history, this script provides the tools you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment