gpt4 book ai didi

bash - 如何将某些命令永久添加到 .bash_history?

转载 作者:行者123 更新时间:2023-11-29 09:26:44 36 4
gpt4 key购买 nike

我使用 bash,有时我不得不为某些任务输入一些很长的命令。这些长命令不经常使用,并且在我再次使用它们时通常会在 .bash_history 文件中被覆盖。如何将某些命令永久添加到 .bash_history?

谢谢。

最佳答案

正如其他人所提到的,通常的做法是将长命令存储为别名或 bash 函数。组织它们的一个好方法是将它们全部放在一个文件中(比如 $HOME/.custom_funcs ),然后从 .bashrc 中获取它。 .

如果您真的希望能够将命令加载到您的 bash 历史记录中,您可以使用 -r history 的选项命令。

来自man page :

-r    Read the current history file and append its contents to the history list. 

只需将所有条目存储在一个文件中,每当您需要加载自定义历史记录时,只需运行 history -r <your_file> .

这是一个演示:

[me@home]$ history | tail  # see current history
1006 history | tail
1007 rm x
1008 vi .custom_history
1009 ls
1010 history | tail
1011 cd /var/log
1012 tail -f messages
1013 cd
1014 ls -al
1015 history | tail # see current history

[me@home]$ cat $HOME/.custom_history # content of custom history file
echo "hello world"
ls -al /home/stack/overflow
(cd /var/log/messages; wc -l *; cd -)

[me@home]$ history -r $HOME/.custom_history # load custom history

[me@home]$ history | tail # see updated history
1012 tail -f messages
1013 cd
1014 ls -al
1015 history | tail # see current history
1016 cat .custom_history
1017 history -r $HOME/.custom_history
1018 echo "hello world"
1019 ls -al /home/stack/overflow
1020 (cd /var/log/messages; wc -l *; cd -)
1021 history | tail # see updated history

请注意如何输入 1018-1020实际上并没有运行,而是从文件中加载。

此时您可以像通常使用 history 一样访问它们或 !命令,或 Ctrl+r 快捷键等。

关于bash - 如何将某些命令永久添加到 .bash_history?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7633026/

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