Skip to content

Instantly share code, notes, and snippets.

@Tolsi
Last active February 27, 2026 03:05
Show Gist options
  • Select an option

  • Save Tolsi/07a6afefd0392b990d40c8234602f589 to your computer and use it in GitHub Desktop.

Select an option

Save Tolsi/07a6afefd0392b990d40c8234602f589 to your computer and use it in GitHub Desktop.
Disabling terminal history and some CLI for this session
#!/usr/bin/env sh
# Disables history saving for the current shell session and common CLI tools.
#
# Usage:
# bash/zsh/ksh: source ~/bin/secure-session.sh
# fish: eval (~/bin/secure-session.sh)
# tcsh/csh: eval `~/bin/secure-session.sh`
#
# When sourced from bash/zsh/ksh, applies settings directly.
# When executed, detects the parent shell and prints eval-able commands.
#
# Disables:
# Shell history (bash, zsh, ksh, fish, tcsh/csh)
# CLI tools history (less, python, node, mysql, psql, sqlite3, redis-cli, gdb, wget)
# --- Detect if executed (not sourced) ---
_secure_session_executed=false
if [ -n "$BASH_VERSION" ]; then
[ "$0" = "${BASH_SOURCE[0]}" ] && _secure_session_executed=true
elif [ -n "$ZSH_VERSION" ]; then
# In zsh, $ZSH_EVAL_CONTEXT contains 'file' when sourced
case "$ZSH_EVAL_CONTEXT" in
*:file) ;;
*) _secure_session_executed=true ;;
esac
elif [ -n "$KSH_VERSION" ]; then
# ksh: heuristic — when executed, $0 contains the script path
case "$0" in
*secure-session*) _secure_session_executed=true ;;
esac
else
# POSIX fallback — assume executed
_secure_session_executed=true
fi
# --- If executed: detect parent shell, print commands, exit ---
if [ "$_secure_session_executed" = true ]; then
_parent=$(ps -o comm= -p "$PPID" 2>/dev/null | sed 's/^-//')
# CLI tools env (common to all shells)
_tools_env='
LESSHISTFILE=/dev/null
PYTHONHISTFILE=/dev/null
NODE_REPL_HISTORY=/dev/null
MYSQL_HISTFILE=/dev/null
PSQL_HISTORY=/dev/null
SQLITE_HISTORY=/dev/null
REDISCLI_HISTFILE=/dev/null
GDBHISTFILE=/dev/null
WGET_HSTS_FILE=/dev/null'
case "$_parent" in
fish)
echo "set -g fish_history ''"
echo "builtin history clear 2>/dev/null"
echo "$_tools_env" | while IFS='=' read -r key val; do
[ -n "$key" ] && echo "set -gx $key $val"
done
echo "echo 'History disabled for this session (shell + CLI tools).'"
;;
tcsh|csh)
echo "unset histfile"
echo "set history=0"
echo "unset savehist"
echo "$_tools_env" | while IFS='=' read -r key val; do
[ -n "$key" ] && echo "setenv $key $val"
done
echo "echo 'History disabled for this session (shell + CLI tools).'"
;;
*)
# bash/zsh/ksh/sh — output POSIX-compatible commands
echo "unset HISTFILE"
echo "HISTSIZE=0"
echo "HISTFILESIZE=0"
echo "export HISTSIZE HISTFILESIZE"
[ "$_parent" = "zsh" ] && cat <<'ZSH'
SAVEHIST=0
unsetopt HISTORY 2>/dev/null
unsetopt SHARE_HISTORY 2>/dev/null
unsetopt INC_APPEND_HISTORY 2>/dev/null
unsetopt INC_APPEND_HISTORY_TIME 2>/dev/null
unsetopt APPEND_HISTORY 2>/dev/null
unsetopt EXTENDED_HISTORY 2>/dev/null
unsetopt HIST_SAVE_NO_DUPS 2>/dev/null
fc -p /dev/null 0 0 2>/dev/null
ZSH
[ "$_parent" = "bash" ] && cat <<'BASH'
set +o history
history -c 2>/dev/null
unset HISTCONTROL HISTIGNORE HISTTIMEFORMAT
BASH
echo "$_tools_env" | while IFS='=' read -r key val; do
[ -n "$key" ] && echo "export $key=$val"
done
echo "echo 'History disabled for this session (shell + CLI tools).'"
;;
esac
exit 0
fi
# --- Sourced mode (bash/zsh/ksh) ---
# Common POSIX
unset HISTFILE
HISTSIZE=0
HISTFILESIZE=0
# zsh
if [ -n "$ZSH_VERSION" ]; then
SAVEHIST=0
unsetopt HISTORY 2>/dev/null
unsetopt SHARE_HISTORY 2>/dev/null
unsetopt INC_APPEND_HISTORY 2>/dev/null
unsetopt INC_APPEND_HISTORY_TIME 2>/dev/null
unsetopt APPEND_HISTORY 2>/dev/null
unsetopt EXTENDED_HISTORY 2>/dev/null
unsetopt HIST_SAVE_NO_DUPS 2>/dev/null
fc -p /dev/null 0 0 2>/dev/null
fi
# bash
if [ -n "$BASH_VERSION" ]; then
set +o history
history -c 2>/dev/null
unset HISTCONTROL
unset HISTIGNORE
unset HISTTIMEFORMAT
fi
# ksh (HISTFILE/HISTSIZE already handled above)
# CLI tools
export LESSHISTFILE=/dev/null
export PYTHONHISTFILE=/dev/null
export NODE_REPL_HISTORY=/dev/null
export MYSQL_HISTFILE=/dev/null
export PSQL_HISTORY=/dev/null
export SQLITE_HISTORY=/dev/null
export REDISCLI_HISTFILE=/dev/null
export GDBHISTFILE=/dev/null
export WGET_HSTS_FILE=/dev/null
unset _secure_session_executed
echo "History disabled for this session (shell + CLI tools)."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment