'history -s' in script not working

I am relatively new to Linux, and am frantically trying to rely on bash and ultimately zsh. In any case, at the moment this has puzzled me:

#!/bin/bash history -s "a_string" 

.... does not work. I've tried a dozen options for an idea, but it doesn't cut anything. Any ideas?

+4
source share
2 answers

The subshell is not interactive and therefore does not save the story, or the parent shell does not reload the story.

Typical methods:

  • use an alias instead of a script

      alias doit='history -s "a_string"' unalias doit 
  • use 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 script instead of executing it in a subshell

     source ./myscript.sh . ./myscript.sh # equivalent shorthand for source 
+2
source

$HISTFILE and $HISTFILESIZE not set when running the script as follows. When you install $HISTFILE , you can read and write the story as you wish.

+1
source

Source: https://habr.com/ru/post/1381049/


All Articles