如何在Linux或macOS终端中使用Bash历史记录

The bash shell is the standard terminal environment included with most Linux distributions, included with macOS, and available for installation on Windows 10. It remembers the commands you type and stores them in a history file. You probably know a few basics of the bash history, but it’s a lot more powerful than you might realize.

bash shell是大多数Linux发行版附带的标准终端环境,macOS附带了该标准终端环境, 可在Windows 10上安装 。 它会记住您键入的命令,并将它们存储在历史文件中。 您可能知道bash历史的一些基础知识,但是它比您可能意识到的强大得多。

键盘快捷键 (Keyboard Shortcuts)

To scroll through your bash history, you can use a few of bash’s many useful keyboard shortcuts. Press these shortcuts and commands you’ve previously used will appear at the prompt.

要滚动查看bash的历史记录,可以使用bash的许多有用的键盘快捷键中的一些 。 按下这些快捷键,您先前使用的命令将出现在提示符下。

  • Up Arrow or Ctrl+P: Go to the previous command in your history. Press the key multiple times to walk backwards through the commands you’ve used.

    向上箭头Ctrl + P :转到历史记录中的上一个命令。 多次按该键可向后浏览您使用的命令。

  • Down Arrow or Ctrl+N: Go to the next command in your history. Press the key multiple times to walk forwards through the commands you’ve used.

    向下箭头Ctrl + N :转到历史记录中的下一个命令。 多次按该键可向前浏览您使用的命令。

  • Alt+R: Revert any changes to a command you’ve pulled from your history if you’ve edited it on the current line.

    Alt + R :如果您在当前行中对其进行了编辑,则将所有更改恢复到您从历史记录中拉出的命令。

Bash also has a special “recall” mode you can use to search for commands you’ve previously run, rather than scrolling through them one by one.

Bash还具有一种特殊的“调用”模式,可用于搜索以前运行的命令,而不是一个个地滚动浏览它们。

  • Ctrl+R: Recall the last command matching the characters you provide. Press this shortcut and start typing to search your bash history for a command.

    Ctrl + R :调用与您提供的字符匹配的最后一个命令。 按此快捷方式并开始键入内容,以在bash历史记录中搜索命令。

  • Ctrl+O: Run the command you found with Ctrl+R.

    Ctrl + O :运行通过Ctrl + R找到的命令。

  • Ctrl+G: Leave the history searching mode without running a command.

    Ctrl + G :不运行命令即可退出历史记录搜索模式。

查看您的Bash历史记录 (View Your Bash History)

You can print your entire bash history to the screen by running a single command:

您可以通过运行单个命令将整个bash历史记录打印到屏幕上:

history

You’ll see a list of all the commands in your bash history, along with a number to the left of each. The command with a “1” next to it is the oldest command in your bash history, while the command with the highest number is the most recent.

您将在bash历史记录中看到所有命令的列表,以及每个命令左侧的数字。 旁边带有“ 1”的命令是bash历史记录中最早的命令,而编号最高的命令是最新的命令。

You can do anything you like with the output. For example, you could pipe it to the grepcommand to search your command history.

您可以使用输出执行任何操作。 例如,您可以将通过管道传递到grep命令以搜索命令历史记录。

history | grep your_search

You could also pipe it to the tail command to view only a small number of the recent commands you’ve run. For example, the following command would show the last 5 entries in your history.

您还可以将其通过管道传递给tail命令,以仅查看您最近运行的少量命令。 例如,以下命令将显示历史记录中的最后5个条目。

history | tail -5

从您的历史记录运行命令 (Run Commands From Your History)

Bash can quickly “expand” previous commands, or expand them and modifying them. This feature is known as “history expansion” and uses an exclamation mark, known as a “bang”. Just type them at the prompt and press Enter to run them like you’d run any other command.

Bash可以快速“扩展”先前的命令,或者对其进行扩展和修改。 此功能称为“历史扩展”,并使用感叹号(称为“爆炸”)。 只需在提示符下键入它们,然后按Enter即可运行它们,就像运行其他命令一样。

To run a specific command from your history by its number, use the following command:

要通过历史记录按编号运行特定命令,请使用以下命令:

!#

For example, let’s say you wanted to run the 12th command from your bash history. That’s the command with a “12” to the left of it when you run the history command. You’d type the following command.

例如,假设您要从bash历史记录中运行第12条命令。 运行history命令时,该命令的左侧带有“ 12”。 您将键入以下命令。

!12

To re-run the last command you ran, type the following. This has the same effect as pressing the Up arrow once to view the previous command and then pressing Enter.

要重新运行上一个运行的命令,请键入以下内容。 这与按一次向上箭头查看上一个命令,然后按Enter的效果相同。

!!

You can also refer to a command a certain number of lines back. For example, !-2 would run the second to last command you ran. !! means the same thing as !-1 .

您还可以引用命令返回一定数量的行。 例如, !-2将运行倒数第二条命令。 !!!-1意思相同

This expansion works anywhere on the line. You can add anything you like before or after !! or any of the other expressions in this section. For example, you can type the following command to rerun the last command you ran through sudo, giving it root privileges. This is particularly useful if you forget to add  sudo before running a command.

该扩展可在生产线上的任何地方工作。 您可以在!!之前或之后添加任何内容!! 或本节中的任何其他表达式。 例如,您可以键入以下命令来重新运行您在sudo运行的最后一个命令,从而为其赋予root特权。 如果您在运行命令之前忘记添加sudo ,这将特别有用。

sudo !!

Or, for example, you could rerun the previous command and pipe its output to grep to search for some text.

或者,例如,您可以重新运行前面的命令并将其输出通过管道传递到grep以搜索某些文本。

!! | grep text

To search for a command in your history and run it, type the following. This will run the last command that matches the text you specify:

要在历史记录中搜索并运行命令,请键入以下内容。 这将运行与您指定的文本匹配的最后一个命令:

!text

So, if you recently ran a command that began with ping, you can run the following command to search for it. This will search backwards through your history, locate the most recent command that begins with “pi“, and immediately run it:

因此,如果您最近运行了以ping开头的命令,则可以运行以下命令进行搜索。 这将向后搜索您的历史记录,找到以“ pi ”开头的最新命令,然后立即运行它:

!pi

You can append a :p to any of the above expansions and bash will print the command to the terminal without running it. This is useful if you want to confirm you’re selecting the correct command before you run it.

您可以将:p附加到上述任何扩展中,并且bash会在不运行命令的情况下将命令打印到终端。 如果要在运行命令之前确认选择了正确的命令,此功能将非常有用。

!12:p
!!:p
!text:p

重用历史记录中的参数 (Reuse Arguments From Your History)

Bash also allows you to run a new command, but use arguments from previous commands in your history. This can help you quickly reuse long or complicated arguments without having to retype them.

Bash还允许您运行新命令,但可以使用历史记录中以前命令的参数。 这可以帮助您快速重用冗长或复杂的参数,而不必重新输入它们。

command !$

For example, let’s say you ran the command touch /home/chris/some_long_file_name_you_dont_want_to_type_again . You now want to run the command nano /home/chris/some_long_file_name_you_dont_want_to_type_again. Rather than typing the whole thing from scratch, you could run:

例如,假设您运行了命令touch /home/chris/some_long_file_name_you_dont_want_to_type_again 。 现在,您要运行命令nano /home/chris/some_long_file_name_you_dont_want_to_type_again 。 不必从头开始输入整个内容,而是可以运行:

nano !$

The !$ would make bash automatically fill in the last argument from your previous command.

!$将使bash自动填充上一个命令的最后一个参数。

This only fills in the last argument. So, if you run ping google.com -c 4 and then run ping !$ , this would just expand to “ping 4“.

这仅填写最后一个参数。 因此,如果您运行ping google.com -c 4然后运行ping !$ ,则它将扩展为“ ping 4 ”。

To fix this situation, you can use the following trick to expand the first argument on the line, rather than the last:

要解决这种情况,可以使用以下技巧来扩展行中的第一个参数,而不是最后一个:

command !^

So, if you ran ping google.com -c 4 and then ran ping !^ , bash would expand this to “ping google.com".

因此,如果您先ping google.com -c 4然后运行ping !^ ,bash会将其扩展为“ ping google.com"

To fill in all the arguments used in the previous command instead of just a single argument, you’d use the following:

要填充上一个命令中使用的所有参数,而不仅仅是单个参数,请使用以下命令:

command !*

So, if you ran ping !* instead, bash would automatically fill in all the arguments you used in the previous command.

因此,如果改为使用ping !* ,则bash会自动填充您在上一个命令中使用的所有参数。

You can use the same trick you use to run commands from your history to get arguments from them. Just use the following form.

您可以使用从历史记录运行命令时使用的相同技巧来获取参数。 只需使用以下形式。

command !abc:#

For example, we ran the command sudo hostname ubuntu earlier. If we run the following command, bash will search backwards through the history to find the last command beginning with the letters we type and fill in the argument we specify. So, if we run echo !su:2 , bash will search back to find the last command beginning with “su” and fill in its second argument, which is “ubuntu“.

例如,我们之前运行了命令sudo hostname ubuntu 。 如果我们运行以下命令,bash将在历史记录中向后搜索,以找到最后一个以我们键入的字母开头的命令,并填写我们指定的参数。 因此,如果我们运行echo !su:2 ,bash将搜索回去以“ su ”开头的最后一个命令,并填写其第二个参数“ ubuntu ”。

Other tricks work as you might expect. For example, replacing the number with an asterisk, known as the wildcard, causes bash to fill in all arguments from the command:

您可能会想到其他技巧。 例如,用星号(通配符)代替数字,将导致bash填充命令中的所有参数:

command !abc:*

重新运行上一个命令并对其进行修改 (Rerun the Previous Command and Modify It)

Bash also allows you to rerun the previous command and specify something that should be changed. This can be useful for correcting a typo in a command. For example, the following command will rerun the previous command, replacing the text “abc” in it with the text “xyz“.

Bash还允许您重新运行上一个命令并指定应更改的内容。 这对于纠正命令中的错字很有用。 例如,以下命令将重新运行上一个命令,将其中的文本“ abc ”替换为文本“ xyz ”。

^abc^xyz

For example, if you accidentally ran ping gogle.com, you could then run ^gog^goog and bash would run the command ping google.com instead.

例如,如果您不小心运行ping gogle.com ,则可以运行^gog^goog而bash会运行ping google.com命令。

您的历史记录存储在哪里,以及如何清除它 (Where Your History Is Stored, and How to Clear It)

The bash shell stores the history of commands you’ve run in your user account’s history file at~/.bash_history by default. For example, if your username is bob, you’ll find this file at /home/bob/.bash_history.

bash shell默认将您已运行的命令的历史记录存储在用户帐户的历史记录文件中,位置为〜/ .bash_history。 例如,如果您的用户名是bob,则可以在/home/bob/.bash_history中找到此文件。

Because your history is stored in a file, it persists between sessions. You can run some commands, sign out, come back the next day, and those commands will still be in your history file ready to view and use. Each user account has its own history file with a separate command history.

因为您的历史记录存储在文件中,所以它在会话之间保持不变。 您可以运行一些命令,退出后再第二天回来,这些命令仍将保留在您的历史记录文件中,可供查看和使用。 每个用户帐户都有其自己的历史记录文件和单独的命令历史记录。

To clear your bash history, you can run the following command. This erases the contents of your user account’s .bash_history file:

要清除bash历史记录,可以运行以下命令。 这将删除用户帐户的.bash_history文件的内容:

history -c

Bash only remembers a limited number of commands by default, preventing the history file from growing too large. The number of history entries bash remembers is controlled by the HISTSIZEvariable. The default is usually 500 or 1000 entries. You can run the following command to view the size of the bash history on your system.

Bash默认情况下仅记住有限数量的命令,以防止历史文件变得太大。 bash记住的历史记录条目数由HISTSIZE变量控制。 默认值通常是500或1000个条目。 您可以运行以下命令来查看系统上bash历史记录的大小。

echo $HISTSIZE

To set your history to zero, run the following command.

要将历史记录设置为零,请运行以下命令。

HISTSIZE=0

For the current session, bash won’t store any history entries unless your run a command like HISTSIZE=1000 to set it back to a certain number of entries.

对于当前会话,除非您运行类似HISTSIZE=1000的命令将其设置回一定数量的条目,否则bash不会存储任何历史记录条目。

如何忽略空格和重复项 (How to Ignore Spaces and Duplicates)

Bash allows you to ignore history entries that begin with a space if you set the HISTCONTROLvariable to ignorespace.

如果将HISTCONTROL变量设置为ignorespace.则Bash允许您忽略以空格开头的历史记录条目ignorespace.

HISTCONTROL=ignorespace

Type a space before a command before running it in the bash shell and the command will run normally, but won’t appear in your history if you have this variable enabled. This allows you to keep your history a bit cleaner, choosing to run commands without them appearing in your history.

在bash shell中运行命令之前,在命令之前输入空格,该命令将正常运行,但是如果启用了此变量,则该命令不会出现在历史记录中。 这样可以使您的历史记录更加整洁,选择运行命令而不使它们出现在历史记录中。

Bash also allows you to ignore duplicate commands that can clutter your history. To do so, set HISTCONTROL to ignoredups.

Bash还允许您忽略可能使您的历史记录混乱的重复命令。 这样做,请将HISTCONTROL设置为ignoredups.

HISTCONTROL=ignoredups

To use both the ignorespace and ignoredups feature, set the HISTCONTROL variable to ignoreboth.

要同时使用ignorespaceignoredups功能,请将HISTCONTROL变量设置为ignoreboth.

HISTCONTROL=ignoreboth

Note that bash variables you set will only persist for the current session. You’ll need to add these to your user account’s .bashrc file to have these values automatically set in every bash session you start, if you prefer that.

请注意,您设置的bash变量将仅在当前会话中保留。 您需要将它们添加到用户帐户的.bashrc文件中,以便在您启动的每个bash会话中自动设置这些值(如果愿意)。

The bash shell is a complex tool with many more options than these. Consult the Bash History Builtins and History Expansion sections in the official bash manual more detailed information and other advanced tricks you can use.

bash shell是一个复杂的工具,具有比这些更多的选择。 请查阅官方bash手册中的Bash历史内置历史扩展部分,以获取更多详细信息和您可以使用的其他高级技巧。

翻译自: https://www.howtogeek.com/howto/44997/how-to-use-bash-history-to-improve-your-command-line-productivity/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值