脚本中的"history -s"不起作用 [英] 'history -s' in a script doesn't work
问题描述
我对linux来说还比较陌生,并且疯狂地尝试精简bash,最后是zsh.无论如何,此刻让我感到难过:
#!/bin/bash历史记录-s"a_string"
....不起作用.我已经对该想法进行了十多种尝试,但没有任何效果.有任何想法吗?
该子外壳程序不是交互式的,因此不会保存历史记录或或父外壳程序不会重新加载历史记录.>
解决此问题的典型方法:
-
使用别名代替脚本
别名doit ='history -s"a_string"'Unalias doit
-
使用shell函数代替脚本
函数doit(){回声一个功能很像一个脚本"history -s但仅在bash命令执行(管道操作)时,才在子shell中运行"}未设置doit
-
source
脚本,而不是在子shell中执行source ./myscript.sh../myscript.sh#等价的简写形式
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 subshellsource ./myscript.sh . ./myscript.sh # equivalent shorthand for source
这篇关于脚本中的"history -s"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!