I'm relatively new to linux, and trying furiously to lean bash, and eventually zsh. Anyway, for the moment this has me stumped:
#!/bin/bash
history -s "a_string"
.... doesn't work. I've tried a dozen variations on the idea, but nothing cuts it. Any ideas?
The subshell is not interactive, and therefore doesn't save the history or the parent shell doesn't reload history.
Typical ways around this:
use an alias instead of a script
alias doit='history -s "a_string"'
unalias doit
use a shell function instead of script
function doit() {
echo "A function is a lot like a script"
history -s "but operates in a subshell only when a bash command does (piping)"
}
unset doit
source
the script, instead of executing it in a subshell
source ./myscript.sh
. ./myscript.sh # equivalent shorthand for source
$HISTFILE
and $HISTFILESIZE
are not set when running a script like this. Once you set $HISTFILE
you can read and write the history as you like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With