diff --git a/tools/erase-toolkit-traces.sh b/tools/erase-toolkit-traces.sh index 8182891..b70294c 100755 --- a/tools/erase-toolkit-traces.sh +++ b/tools/erase-toolkit-traces.sh @@ -122,60 +122,36 @@ if [ "$CLEAN_HISTORY" = true ] && [ -f ~/.bash_history ]; then echo "" echo "→ Final cleanup: Removing bash history..." - # Disable history recording for this session to prevent re-adding commands + # Disable history recording AND appending for this session set +o history + shopt -u histappend 2>/dev/null || true - # Clean in-memory history first using history -d (most reliable method) - echo " → Cleaning in-memory history..." + echo " → Cleaning history file..." GREP_PATTERN="git\.mull\.lol|linux-server-management-toolkit|server-toolkit|launcher\.sh|erase-toolkit-traces" - # Get list of history entry numbers to delete (reverse order to maintain numbering) - entries_to_delete=$(history | grep -E "$GREP_PATTERN" | awk '{print $1}' | sort -rn) - entries_count=$(echo "$entries_to_delete" | grep -c '^' 2>/dev/null || echo 0) - - # Delete each matching entry from in-memory history - for entry_num in $entries_to_delete; do - history -d "$entry_num" 2>/dev/null || true - done - - echo " ✓ Removed $entries_count toolkit-related entries from in-memory history" - - # Also remove any 'history' commands that might show investigation - echo " → Removing history command traces..." - history_entries=$(history | grep -E '^\s*[0-9]+\s+.*history' | awk '{print $1}' | sort -rn) - history_count=$(echo "$history_entries" | grep -c '^' 2>/dev/null || echo 0) - - for entry_num in $history_entries; do - history -d "$entry_num" 2>/dev/null || true - done - - if [ "$history_count" -gt 0 ]; then - echo " ✓ Removed $history_count history command entries" - fi - - # Write cleaned in-memory history back to file - history -w - - echo " ✓ Cleaned history written to file" - - # Also clean the history file directly (in case commands are from other sessions) - echo " → Cleaning history file for commands from other sessions..." + # Clean the history file directly if [ -f ~/.bash_history ]; then cp ~/.bash_history ~/.bash_history.bak.$$ + lines_before=$(wc -l < ~/.bash_history.bak.$$ 2>/dev/null || echo 0) grep -Ev "$GREP_PATTERN" ~/.bash_history.bak.$$ > ~/.bash_history 2>/dev/null || true + lines_after=$(wc -l < ~/.bash_history 2>/dev/null || echo 0) + lines_removed=$((lines_before - lines_after)) rm -f ~/.bash_history.bak.$$ - echo " ✓ History file cleaned" + echo " ✓ Removed $lines_removed entries from history file" fi - # Reload cleaned history into current session to prevent re-adding on exit - echo " → Reloading cleaned history into current session..." + # Clear current session's history completely to prevent re-adding on exit + echo " → Clearing current session history..." history -c - history -r - echo " ✓ In-memory history reloaded from cleaned file" + # Unset HISTFILE to prevent this session from writing on exit + unset HISTFILE + + echo " ✓ Current session history cleared and disabled" echo "" - echo "NOTE: For complete cleanup, run 'exec bash' to start a fresh shell." - echo " This prevents any lingering history from being re-saved." + echo -e "${YELLOW}IMPORTANT: Exit this shell immediately after cleanup${NC}" + echo "Type: exit" + echo "Then start a fresh shell to see cleaned history." fi # Offer to remove the entire toolkit (AFTER history cleaning)