Bash Script to Save User Session Tool System Prompt
/ 3 min read /
echo "# Functions" >> "$SNAPSHOT_FILE"
# Force autoload all functions firsttypeset -f > /dev/null 2>&1
# Now get user function names - filter system ones and write directly to filetypeset +f | grep -vE '^(_|__)' | while read func; dotypeset -f "$func" >> "$SNAPSHOT_FILE"done`;else Q+=`echo "# Functions" >> "$SNAPSHOT_FILE"
# Force autoload all functions firstdeclare -f > /dev/null 2>&1
# Now get user function names - filter system ones and give the rest to eval in b64 encodingdeclare -F | cut -d' ' -f3 | grep -vE '^(_|__)' | while read func; do# Encode the function to base64, preserving all special charactersencoded_func=$(declare -f "$func" | base64 )# Write the function definition to the snapshotecho "eval ${sZ0}"${sZ0}$(echo '$encoded_func' | base64 -d)${sZ0}" > /dev/null 2>&1" >> "$SNAPSHOT_FILE"done`;if(B)Q+=`echo "# Shell Options" >> "$SNAPSHOT_FILE"setopt | sed 's/^/setopt /' | head -n 1000 >> "$SNAPSHOT_FILE"`;else Q+=`echo "# Shell Options" >> "$SNAPSHOT_FILE"shopt -p | head -n 1000 >> "$SNAPSHOT_FILE"set -o | grep "on" | awk '{print "set -o " $1}' | head -n 1000 >> "$SNAPSHOT_FILE"echo "shopt -s expand_aliases" >> "$SNAPSHOT_FILE"`;return Q+=`echo "# Aliases" >> "$SNAPSHOT_FILE"# Filter out winpty aliases on Windows to avoid "stdin is not a tty" errors# Git Bash automatically creates aliases like "alias node='winpty node.exe'" for# programs that need Win32 Console in mintty, but winpty fails when there's no TTYif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; thenalias | grep -v "='winpty " | sed 's/^alias //g' | sed 's/^/alias -- /' | head -n 1000 >> "$SNAPSHOT_FILE"elsealias | sed 's/^alias //g' | sed 's/^/alias -- /' | head -n 1000 >> "$SNAPSHOT_FILE"fi
# Check for rg availabilityecho "# Check for rg availability" >> "$SNAPSHOT_FILE"echo "if ! command -v rg >/dev/null 2>&1; then" >> "$SNAPSHOT_FILE"echo ' alias rg='"'${B.replace(/'/g,"'\\''")}'" >> "$SNAPSHOT_FILE"echo "fi" >> "$SNAPSHOT_FILE"
# Add PATH to the fileecho "export PATH=${N51.default.quote([A||""])}" >> "$SNAPSHOT_FILE"
${Q?`source "${D}" < /dev/null`:"# No user config file to source"}
# First, create/clear the snapshot fileecho "# Snapshot file" >| "$SNAPSHOT_FILE"
# When this file is sourced, we first unalias to avoid conflicts# This is necessary because aliases get "frozen" inside function definitions at definition time,# which can cause unexpected behavior when functions use commands that conflict with aliasesecho "# Unset all aliases to avoid conflicts with functions" >> "$SNAPSHOT_FILE"echo "unalias -a 2>/dev/null || true" >> "$SNAPSHOT_FILE"
${G}
${F}