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

linux – 在bash别名或函数中使用历史扩展

发布时间:2020-12-14 00:32:42 所属栏目:Linux 来源:网络整理
导读:我想做一件简单的事情让我的队友生活更轻松.他们不断地将报价复制到命令行中,这些命令行的格式化会破坏命令,即:“test”与“test” 事实证明,令人惊讶的是: function damn() { !!:gs/“/" } 要么: alias damn='!!:gs/“/"' 似乎都没有工作,并一直给我错误
我想做一件简单的事情让我的队友生活更轻松.他们不断地将报价复制到命令行中,这些命令行的格式化会破坏命令,即:“test”与“test”

事实证明,令人惊讶的是:

function damn() { !!:gs/“/" }

要么:

alias damn='!!:gs/“/"'

似乎都没有工作,并一直给我错误

-bash: !!:gs/“/" : No such file or directory

要不就:

>

我必须在这里遗漏一些明显的东西.

解决方法

!在函数或别名中不起作用.根据bash手册:

History expansion is performed immediately after a complete line is read,before the shell breaks it into words.

您可以使用builtin fc命令:

[STEP 100] # echo $BASH_VERSION
4.4.19(1)-release
[STEP 101] # alias damn='fc -s “=" ”=" '
[STEP 102] # echo “test”
“test”
[STEP 103] # damn
echo "test"
test
[STEP 104] #

为了快速引用,以下是help fc的输出.

fc: fc [-e ename] [-lnr] [first] [last] or fc -s [OLD=NEW] [command]
    Display or execute commands from the history list.

    fc is used to list or edit and re-execute commands from the history list.
    FIRST and LAST can be numbers specifying the range,or FIRST can be a
    string,which means the most recent command beginning with that
    string.

    Options:
      -e ENAME  select which editor to use.  Default is FCEDIT,then EDITOR,then vi
      -l        list lines instead of editing
      -n        omit line numbers when listing
      -r        reverse the order of the lines (newest listed first)

|   With the `fc -s [OLD=NEW ...] [command]' format,COMMAND is
|   re-executed after the substitution OLD=NEW is performed.

    A useful alias to use with this is r='fc -s',so that typing `r cc'
    runs the last command beginning with `cc' and typing `r' re-executes
    the last command.

    Exit Status:
    Returns success or status of executed command; non-zero if an error occurs.

(编辑:李大同)

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

    推荐文章
      热点阅读