我对 linux 比较陌生,并且疯狂地尝试精益 bash,最终是 zsh。无论如何,目前这让我很难过:
#!/bin/bash
history -s "a_string"
Run Code Online (Sandbox Code Playgroud)
.... 不起作用。我已经对这个想法尝试了十几种变体,但没有任何效果。有任何想法吗?
子 shell 不是交互式的,因此不会保存历史记录,或者父 shell 不会重新加载历史记录。
解决此问题的典型方法:
使用别名而不是脚本
alias doit='history -s "a_string"'
unalias doit
Run Code Online (Sandbox Code Playgroud)使用 shell 函数代替脚本
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
Run Code Online (Sandbox Code Playgroud)source
脚本,而不是在子 shell 中执行它
source ./myscript.sh
. ./myscript.sh # equivalent shorthand for source
Run Code Online (Sandbox Code Playgroud)