gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:03:23 26 4
gpt4 key购买 nike

我正在尝试做一件简单的事情,让我的队友生活得更轻松。他们不断地将引号复制到格式化的命令行中,这会破坏命令,即:“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.

你可以使用内置的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.

关于linux - 在 bash 别名或函数中使用历史扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48696876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com