Created
March 27, 2025 08:42
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
--iso
), UTC (--utc
), or local time (--local
), or define your own custom format using--format
.America/New_York
) using the--tz
option to ensure timestamps are displayed in your desired region.HISTTIMEFORMAT
andTZ
environment variables without affecting your shell's persistent configuration.--help
option, ensuring ease of use for both sourced and direct execution.Usage Examples:
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.