<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bash | Easton Man's Blog</title>
	<atom:link href="https://blog.eastonman.com/blog/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.eastonman.com</link>
	<description>临渊羡鱼，不如退而结网</description>
	<lastBuildDate>Thu, 10 Jun 2021 14:51:36 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.5</generator>

<image>
	<url>https://blog.eastonman.com/wp-content/uploads/2021/02/cropped-Logo-e1613298891313-32x32.png</url>
	<title>Bash | Easton Man's Blog</title>
	<link>https://blog.eastonman.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Bash History的使用技巧</title>
		<link>https://blog.eastonman.com/blog/2021/06/bash-history-tricks/</link>
					<comments>https://blog.eastonman.com/blog/2021/06/bash-history-tricks/#comments</comments>
		
		<dc:creator><![CDATA[Easton Man]]></dc:creator>
		<pubDate>Thu, 10 Jun 2021 14:51:35 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">https://blog.eastonman.com/?p=792</guid>

					<description><![CDATA[<p>Bash是常见的shell环境，大多数Linux发行版也都带有Bash，因此高效地利用history可以节省手动输入重复命令的时间。本文分享一下几个我自己使用的技巧和配置。</p>
The post <a href="https://blog.eastonman.com/blog/2021/06/bash-history-tricks/">Bash History的使用技巧</a> first appeared on <a href="https://blog.eastonman.com">Easton Man's Blog</a>.]]></description>
										<content:encoded><![CDATA[<p class="wpwc-reading-time">预计阅读时间： 3 分钟</p>
<p>Bash是常见的shell环境，大多数Linux发行版也都带有Bash，因此高效地利用history可以节省手动输入重复命令的时间。本文分享一下几个我自己使用的技巧和配置。</p>



<h3 class="wp-block-heading">搜索</h3>



<p>这个技巧大家应该多少都知道，就是在输入界面按<code>Ctrl-R</code>就可以进行搜索了，返回的结果是最近一个匹配的命令，然后按回车就可以执行。</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="526" height="220" src="https://blog.eastonman.com/wp-content/uploads/2021/06/image.png" alt="" class="wp-image-794" srcset="https://blog.eastonman.com/wp-content/uploads/2021/06/image.png 526w, https://blog.eastonman.com/wp-content/uploads/2021/06/image-300x125.png 300w" sizes="(max-width: 526px) 100vw, 526px" /><figcaption>Ctrl-R</figcaption></figure></div>



<p>但是有的时候搜索结果只是与我想要的很相近，但又不完全一样，我希望能够在执行之前对命令做小的修改，那怎么办?其实此时按Esc就可以退出并且编辑。</p>



<p>还有一个方式是利用<code>:p</code>，<code>:p</code>在Bash中表示打印但不执行，因此如果我们想要查看某个前缀的命令，就可以使用<code>!prefix:p</code>来打印。</p>



<h3 class="wp-block-heading">HISTIGNORE</h3>



<p>HISTIGNORE是一个环境变量，它指示Bash什么样的命令不要加入history中，例如这样的一个变量。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">export HISTIGNORE='pwd:exit:fg:bg:top:clear:history:ls:uptime:df'</pre>



<p>就可以把无意义的交互命令从history中去除了。如果你经常使用<code>ll</code>或者<code>la</code>这样的简写，那么把他们加入HISTIGNORE中或许是一个不错的选择。</p>



<h3 class="wp-block-heading">HISTCONTROL</h3>



<p>HISTCONTROL也是一个环境变量，它有几个选择</p>



<ul class="wp-block-list"><li>ignorespace：以空格开头的所有命令都不会加入history中</li><li>ignoredups：重复的命令不会加入history中</li><li>ignoreboth：打开以上的两个特性</li></ul>



<p>通常一些发行版已经带有ignoredups的选项，如果你想要避免某些特定的命令计入history中，那么可以打开ignorespace选项。</p>



<h3 class="wp-block-heading">并行的窗口之间的history问题</h3>



<p>这个问题相信很多同学也会有，在远程管理服务器的时候，通常为了某些监视任务或者耗时长的任务不阻塞人的操作，会使用各种窗口复用工具，比如<code>tmux</code>或者<code>screen</code>，那么由于Bash在关闭的时候才会将history写入<code>.bash_history</code>文件，那么大多数时候当我们关闭这些窗口以后就只有最后一个关闭的窗口的History被记录了下来。</p>



<p>我们可以使用</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">shopt -s histappend</pre>



<p>来指示Bash讲history附加到原本的history上而不是覆盖。</p>



<h3 class="wp-block-heading">使用!来执行history中的命令</h3>



<p>!可以用来执行命令，比如!10就是执行history中的第10条命令。当然也可以执行最近的第几条命令，比如!-3就是倒数第三条命令。</p>



<p>这里还可以结合<code>:p</code>使用，以避免直接执行带来的风险。</p>



<p>如果你一定要避免所有的风险，每次都希望检查命令再执行，而又不想输入<code>:p</code>，那么Bash还有一个选项可以做到这一点：<code>shopt -s histverify</code>。这个选项会使得所有用!执行的命令都需要按Enter键确认才会执行。</p>



<h3 class="wp-block-heading">使用上一个命令的参数</h3>



<p>!$和!*分别会将上一个命令的最后一个参数和所有参数填补到这个位置，例如</p>



<pre class="EnlighterJSRAW" data-enlighter-language="shell" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$ mv list.txt items.txt
$ vim !$
vim items.txt
$ cp !$ shopping.txt
cp items.txt shopping.txt</pre>



<p></p>The post <a href="https://blog.eastonman.com/blog/2021/06/bash-history-tricks/">Bash History的使用技巧</a> first appeared on <a href="https://blog.eastonman.com">Easton Man's Blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.eastonman.com/blog/2021/06/bash-history-tricks/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
