脚本中的"history -s"不起作用 [英] 'history -s' in a script doesn't work

查看:42
本文介绍了脚本中的"history -s"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对linux来说还比较陌生,并且疯狂地尝试精简bash,最后是zsh.无论如何,此刻让我感到难过:

 #!/bin/bash历史记录-s"a_string" 

....不起作用.我已经对该想法进行了十多种尝试,但没有任何效果.有任何想法吗?

解决方案

该子外壳程序不是交互式的,因此不会保存历史记录或父外壳程序不会重新加载历史记录.

解决此问题的典型方法:

  1. 使用别名代替脚本

     别名doit ='history -s"a_string"'Unalias doit 

  2. 使用shell函数代替脚本

     函数doit(){回声一个功能很像一个脚本"history -s但仅在bash命令执行(管道操作)时,才在子shell中运行"}未设置doit 

  3. 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:

  1. use an alias instead of a script

       alias doit='history -s "a_string"'  
       unalias doit
    

  2. 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
    

  3. source the script, instead of executing it in a subshell

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

这篇关于脚本中的"history -s"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆