I was looking for something similar and could not find it, so I made two scenarios below. To get started, just say shellstate and then at least set -i and set -o emacs , which this reset_shellstate does not do for you. I do not know how to set bash variables which, in his opinion, are special.
~/bin/reset_shellstate :
#!/bin/bash __="$PWD/shellstate_${1#_}" trap ' declare -p >"'"$__"'" trap >>"'"$__"'" echo cd \""$PWD"\" >>"'"$__"'" # setting PWD did this already, but... echo set +abefhikmnptuvxBCEHPT >>"'"$__"'" echo set -$- >>"'"$__"'" # must be last before sed, see $s/s//2 below sed -ri '\'' $s/s//2 s,^trap --,trap, /^declare -[^ ]*r/d /^declare -[^ ]* [A-Za-z0-9_]*[^A-Za-z0-9_=]/d /^declare -[^ ]* [^= ]*_SESSION_/d /^declare -[^ ]* BASH[=_]/d /^declare -[^ ]* (DISPLAY|GROUPS|SHLVL|XAUTHORITY)=/d /^declare -[^ ]* WINDOW(ID|PATH)=/d '\'' "'"$__"'" shopt -op >>"'"$__"'" shopt -p >>"'"$__"'" declare -f >>"'"$__"'" echo "Shell state saved in '"$__"'" ' 0 unset __
~/bin/shellstate :
#!/bin/bash shellstate=shellstate_${1#_} test -s $shellstate || reset_shellstate $1 shift bash --noprofile --init-file shellstate_${1#_} -is " $@ " exit $?
jthill
source share