I've lost long commands I've typed for 3-5 mins in previous run of Termux. How to preserve all commands I type like .bash_history?
You can extend the -n value for the Bash environment variables, HISTFILESIZE & HISTSIZE in ${PREFIX}/etc/bash.bashrc or ~/.bash_profile - more a matter of personal preference in the case of Termux, as it sets up a single user, however extensible, environment by default. Otherwise, bash.bashrc houses global, system wide variables while .bash_profile - created in the user's $HOME directory - houses the shell variables for each user account, independent of $SHELL's globally set functions & variables.
FYI: Each of the above mentioned, configurable env var is set to 500 - globally - by default. Run the set command without options or arguments to see $SHELL's system wide env var printed to stdout. Run printenv to view user-defined variables instead.
Nice explanation @tinydynamite00! Closing this issue.
$ echo "$SHELL"
/data/data/com.termux/files/usr/bin/bash
$ printenv | grep HIST
$ set | grep HIST
HISTFILE=/data/data/com.termux/files/home/.bash_history
HISTFILESIZE=500
HISTSIZE=500
Commands are written to history only if I've type exit before closing application. Is it possible to catch Android event of application closing and call exit in terminal?
Several shopt settings are off by default, primarily for privacy reasons. Run shopt at a prompt (w/o options or arguments). Is the shell option histappend turned on for your shell? Without this option on, history is overwritten when a new shell is spawned. Also, set will inform you how history is being managed with the value of the shell variable HISTCONTROL.
This, from the _GNU Bash Reference Manual_ may be of interest to you:
HISTCONTROL
A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ‘ignorespace’, lines which begin with a space character are not saved in the history list. A value of ‘ignoredups’ causes lines which match the previous history entry to not be saved. A value of ‘ignoreboth’ is shorthand for ‘ignorespace’ and ‘ignoredups’. A value of ‘erasedups’ causes all previous lines matching the current line to be removed from the history list before that line is saved. Any value not in the above list is ignored. If HISTCONTROL is unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIGNORE. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL.
I've lost long commands I've typed for 3-5 mins in previous run of Termux. How to preserve all commands I type like .bash_history?
Do echo "shopt -s histappend" >> ~/.bash_profile. And then use exit to save the history on exit from the shell.
Most helpful comment
You can extend the -n value for the Bash environment variables,
HISTFILESIZE&HISTSIZEin${PREFIX}/etc/bash.bashrcor~/.bash_profile- more a matter of personal preference in the case of Termux, as it sets up a single user, however extensible, environment by default. Otherwise,bash.bashrchouses global, system wide variables while.bash_profile- created in the user's$HOMEdirectory - houses the shell variables for each user account, independent of$SHELL's globally set functions & variables.FYI: Each of the above mentioned, configurable env var is set to 500 - globally - by default. Run the
setcommand without options or arguments to see$SHELL's system wide env var printed tostdout. Runprintenvto view user-defined variables instead.