A shell exclamation mark is not for yelling. Be lazy
A shell exclamation mark is not for yelling. Be lazy.
Shell 中的感叹号不是用来大喊大叫的,请学会偷懒。
Event designators have been hiding in not-so-plain-sight since the late 1970s — so powerful that forgetting might be part of a massive conspiracy to wear out developer keyboards faster than otherwise. 事件指示符(Event designators)自 20 世纪 70 年代末以来就一直隐藏在“并不那么显眼”的地方——它们功能强大,以至于人们遗忘它们可能是一场巨大的阴谋,旨在比正常情况下更快地磨损开发者的键盘。
Don’t yell at your colleagues; yell in your shell
不要对同事大喊大叫,要在 Shell 里喊
You and I are probably very different; my choice of editor is likely to make you cringe just thinking about it, and your favorite programming language is statistically — definitely — not mine. There is however one thing (and maybe that’s the only thing) we all have in common; we are a bunch of lazy bastards. 你和我可能大不相同;我选择的编辑器可能让你光是想想就感到难受,而你最喜欢的编程语言在统计学上——绝对——不是我喜欢的。然而,我们所有人都有一个共同点(也许也是唯一的一点):我们都是一群懒惰的家伙。
Note: This article is primarily targeting bash, csh, tcsh, and zsh. If these shells are not your daily driver, see POSIX and the power of fc — worth a read no matter which POSIX-compatible shell you use. 注:本文主要针对 bash、csh、tcsh 和 zsh。如果这些 Shell 不是你的日常工具,请参阅“POSIX 和 fc 的威力”——无论你使用哪种兼容 POSIX 的 Shell,都值得一读。
Command-line repetition we socially accept
我们在社交上接受的命令行重复
I am sure we have all ended up in arrow-up-mashing hell when repeating commands in our shell history, but what if we instead could save ourselves the trouble and refer to previous commands and their arguments directly? 我相信我们都曾陷入过在 Shell 历史记录中疯狂按“上箭头”键的噩梦,但如果我们能省去这些麻烦,直接引用之前的命令及其参数,岂不是更好?
No yelling — just exclamation marks. Note: The magic described in this article applies to interactive shells unless configured otherwise, it is not meant to be used in your new amazing.sh shell script.
没有大喊大叫,只有感叹号。注:本文描述的魔法适用于交互式 Shell(除非另有配置),不建议在你的 amazing.sh 脚本中使用。
Don’t Repeat Yourself.
不要重复你自己 (DRY)。
I bet you’ve heard it before, “do not repeat yourself”, either from that annoying colleague with all the obscure magic (who might smell a little funny) — or perhaps you said it yourself during the last refactor session that somehow lasted longer than the federation delay on matrix.org. 我敢打赌你以前听过“不要重复你自己”这句话,要么是来自那个掌握各种晦涩魔法(可能还有点怪味)的讨厌同事,要么是你自己在上次重构会议上说的——那次会议持续的时间比 matrix.org 的联邦延迟还要长。
Most of us have heard DRY repeated since we were ki–, I mean junior. But is it not funny that we’ve religiously been taught to apply DRY everywhere… except on the command‑line? 我们大多数人从还是“小屁孩”——我是说初级工程师时——就开始听 DRY 了。但有趣的是,我们被教导要在任何地方都虔诚地应用 DRY 原则……除了在命令行上?
What is this magic an event designator?
这种名为“事件指示符”的魔法是什么?
Even though they might look mighty daunting at first glance, event designators always follow the same rather simple pattern: 尽管它们乍一看可能非常令人生畏,但事件指示符总是遵循同样相当简单的模式:
![event][:word][:modifier]
Note: Want to know the bare necessities? Check out “yell responsibly” for the 4 event designators I consider most useful. 注:想了解最基本的需求吗?查看“负责任地大喊”部分,了解我认为最有用的 4 个事件指示符。
Event Designator
事件指示符
The first part immediately following the exclamation mark denotes which lines we are interested in — here are a few examples which will effectively rerun the referred to command: 紧跟在感叹号后的第一部分表示我们感兴趣的行——以下是一些可以有效重新运行所引用命令的示例:
!!: the previous line (上一行)!-2: two lines back (倒数第二行)!1337: the 1337th line, as displayed byhistory(历史记录中的第 1337 行)!ssh: the most recent line starting with “ssh” (最近一行以 “ssh” 开头的命令)!?dandelion?: the most recent line containing “dandelion” (最近一行包含 “dandelion” 的命令)
Note: If the ! is immediately followed by :, no explicit event is specified and the expression will refer to the immediately previous line.
注:如果 ! 后面紧跟 :,则未指定明确的事件,表达式将引用紧邻的前一行。
Word Designator
单词指示符
After the event designator (if any) you may specify which part of the line you are interested in. 在事件指示符(如果有的话)之后,你可以指定你感兴趣的行中的哪一部分。
!:0: 1st word (第一个单词)!:1: 2nd word (第二个单词)!:$: last word (最后一个单词)!:1-2: 2nd to 3rd (第 2 到第 3 个单词)!:*: all args (所有参数)
Modifier
修饰符
Modifiers are perhaps where we enter “oh-daaaaaaaym”-territory — they allow you to extract/modify only certain parts of what would otherwise be an entire argument. 修饰符可能是我们进入“哇哦”领域的关键——它们允许你仅提取或修改原本完整参数中的特定部分。
!:$:h: strip filename (dirname) (去除文件名,获取目录路径)!:1:t: strip leading path (basename) (去除路径,获取文件名)!:1:r: strip only the extension (仅去除扩展名)!:1:e: leave only the extension (仅保留扩展名)!ssh:p: print what would run, without running it (打印将要运行的命令,但不执行)
POSIX and the power of fc
POSIX 和 fc 的威力
Whether or not your shell speaks !, fc belongs in your imaginary tool belt. It can be used to repeat the previous command, but the true power lies in the fact that you can edit your command…
无论你的 Shell 是否支持 !,fc 都应该出现在你的工具箱里。它不仅可以用来重复上一条命令,其真正的威力在于你可以编辑你的命令……