加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 脚本中的’history -s’不起作用

发布时间:2020-12-15 20:57:37 所属栏目:安全 来源:网络整理
导读:我对 linux比较陌生,并且疯狂地尝试精简bash,最终zsh.无论如何,目前这让我难过: #!/bin/bashhistory -s "a_string" ….不起作用.我已经尝试了十几种不同的想法,但没有任何削减它. 有任何想法吗? 解决方法 子shell不是交互式的,因此不保存历史记录或父shell
我对 linux比较陌生,并且疯狂地尝试精简bash,最终zsh.无论如何,目前这让我难过:

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

….不起作用.我已经尝试了十几种不同的想法,但没有任何削减它.
有任何想法吗?

解决方法

子shell不是交互式的,因此不保存历史记录或父shell不重新加载历史记录.

典型的解决方法:

>使用别名而不是脚本

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

>使用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

>获取脚本,而不是在子shell中执行它

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读