Skip to content

Commit

Permalink
你应该知道的 3 个罕见的 Bash 技巧 (#7896)
Browse files Browse the repository at this point in the history
* 添加校对者信息:)

* 翻译完成

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Update article/2021/3-uncommon-bash-tricks-that-you-should-know.md

Co-authored-by: Kim Zhou <[email protected]>

* Fix minor problems

Co-authored-by: flashhu <[email protected]>
Co-authored-by: Kim Zhou <[email protected]>

* Update Proofreaders information

Co-authored-by: flashhu <[email protected]>
Co-authored-by: Kim Zhou <[email protected]>
Co-authored-by: lsvih <[email protected]>

Co-authored-by: Kim Zhou <[email protected]>
Co-authored-by: flashhu <[email protected]>
Co-authored-by: Kim Zhou <[email protected]>
Co-authored-by: lsvih <[email protected]>
  • Loading branch information
5 people authored Jan 26, 2021
1 parent 3314f9b commit 9b0049c
Showing 1 changed file with 60 additions and 64 deletions.
124 changes: 60 additions & 64 deletions article/2021/3-uncommon-bash-tricks-that-you-should-know.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,191 +2,187 @@
> * 原文作者:[Adam Green](https://medium.com/@adgefficiency)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/article/2021/3-uncommon-bash-tricks-that-you-should-know.md](https://github.com/xitu/gold-miner/blob/master/article/2021/3-uncommon-bash-tricks-that-you-should-know.md)
> * 译者:
> * 校对者:
> * 译者:想念苏苏的 [PassionPenguin](https://github.com/PassionPenguin)
> * 校对者:[flashhu](https://github.com/flashhu)[Kim Zhou](https://github.com/plusmultiply0)
# 3 Uncommon Bash Tricks That You Should Know
# 3 个鲜为人知的 Bash 技巧

![Image by author — Matera, Italy](https://cdn-images-1.medium.com/max/4000/0*-UdH52A57htDgdu0.png)
![图源 Matera,摄于意大利](https://cdn-images-1.medium.com/max/4000/0*-UdH52A57htDgdu0.png)

**Good developers type less** — allowing them to:
优秀开发人员键入的内容更少 —— 这让他们:

* Work faster
* Work more accurately
* Make fewer mistakes
* Reduce stress on their ever-so-tired hands
* 工作更快
* 更准确地工作
* 减少错误
* 减轻双手疲劳的压力

One way to type less is to make proper use of your shell (commonly Bash). **That’s what this post is about — three Bash tricks that will allow you to type less.**
一种减少键入的方法是恰当地使用你的命令行(通常是 Bash)。**这就是本文章要介绍的 —— 3 个减少键入的 Bash 技巧。**

The three Bash tips covered in this post are:
本文中介绍的 3 个 Bash 技巧是:

1. **Parameter expansion** with `{a,b}` to avoid retyping on a single command
2. **Accessing the last argument** with `$_` to avoid retyping from the last command
3. **Quick substitution** with `^old^new` to quickly change part of the last command
1. 使用 `{a,b}`— 扩展参数,以避免再次键入单个命令
2. 使用 `$_`— 访问最后一个参数,以避免从最后一个命令中重新键入
3. 使用 `^old^new`— 快速更改最后一条命令的一部分

All of these tricks are also compatible with zsh.
所有这些技巧都与 zsh 兼容。

**This post follows the convention of starting shell commands with a** `$`. **For commands that would be expanded by the shell, I will show the expanded command below without a** `$`.
**本文输入的命令均以 `$` 开头。对于命令行解析后的代码,我将在相关代码下面不带 `$` 扩展命令。**

## Parameter Expansion With {a,b}
## `{a,b}` 拓展参数

When writing a single command, it’s common to repeat yourself.
当我们在编写命令时,我们常会重复输入同一个命令。

Take the example of changing the suffix on a file, which we can do using `mv`:
以更改文件后缀为例,我们可以使用 `mv`

```bash
$ mv README.txt README.md
```

Notice how we write `README` twice?

Parameter expansion will avoid this repetition — allowing us to change the suffix on our file without typing `README` twice:
注意到我们写了两次 README,而参数扩展可以避免这种重复 —— 更改文件的后缀而无需输入 README 两次:

```bash
$ mv README.{txt,md}
mv README.txt README.md
```

The parameter expansion we use is `{txt,md}`, which is expanded to two arguments — `txt md` (separated by a space).
我们使用的参数扩展为 `{txt,md}`,而它扩展为两个参数 —— `txt md`(以空格分隔)。

**Parameter expansion creates one argument for each element inside the curly braces, separated by a comma**:
**参数扩展会为花括号内,以逗号分割的每个元素分别创建一个参数:**

```bash
$ echo {1,2,3}
1 2 3

$ echo pre{1,2,3}
pre1fix pre2fix pre3fix
$ echo pre{1,2,3}
pre1 pre2 pre3
```

An empty entry will create an argument with nothing substituted:
空元素将创建不带任何替换项的参数:

```bash
$ echo pre{,1,2}fix
prefix pre1fix pre2fix
```

Another example — renaming a `models` folder to `ml` inside a `data` folder:
另一个示例 —— 将 `data` 文件夹中的 `models` 文件夹重命名为 `ml`

```bash
$ mv data/models data/ml
```

We can save retyping `data/` by using parameter expansion:
我们可以使用参数扩展来节省重新输入 `data/` 的时间:

```bash
$ mv data/{models,ml}
mv data/models data/ml
```

We can use parameter expansion with a sequence of numbers — useful to create numbered directories:
我们可以使用带有数字序列的参数扩展 —— 这在创建顺序编号目录时很有用:

```bash
$ mkdir data{0..2}
mkdir data0 data1 data2
```

**We can also do parameter expansion inside an argument** for example, to change a folder halfway up a path:
我们还可以在**参数内部进行参数扩展**— 例如,更改路径中的文件夹名称:

```bash
$ cat models/{baseline,final}/data.csv
cat models/baseline/data.csv models/final/data.csv
```

A final example, using three parameters — moving two Python test files into a `tests` folder:
最后一个示例:通过 `mv` 命令使用了 3 个参数 —— 将两个 Python 文件移动到 tests 文件夹中:

```bash
$ mv test_unit.py test_system.py tests
```

#### Summary
#### 小结

**Any time you are retyping something multiple times in a single command, it’s likely parameter expansion can help save your exhausted hands.**
**每当你在单个命令中多次键入某些内容时,参数扩展极大可能可以帮助你节省精力。**

## Accessing the Last Argument With $_
## 使用 `$_` 使用访问最后一个参数

Terminals are operated by a sequence of commands — we often reuse information across multiple commands.
终端由一系列命令操作——我们经常在多个命令之间重用信息。

Our previous tip, parameter expansion, is about typing less on a single command — **this tip is about typing less across multiple commands**.
我们前面的技巧,参数扩展,用于减少在单个命令上的键入 —— 现在这个技巧则是用于减少对多个命令的键入。

Take the simple case of making a folder and moving into it:
以创建文件夹并将当前目录转移到其中的情况为例:

```bash
$ mkdir temp
$ cd temp
```

Notice that we reuse the argument `temp` again in our second command?

**We can save retyping** `temp` **and bring it forward from the previous command using** `$_`:
**我们可以通过使用 `$_` 将上个命令的参数传递过来,避免让自己重复键入同样的内容:**

```bash
$ mkdir temp
$ cd $_
cd temp
```
```bash
$ mkdir temp
$ cd $_
cd temp
```

Above, we use `$_` to access the last argument of the previous command, which in this case is `temp`.
上面的代码中,我们使用了 `$_` 访问上一个命令的最后一个参数,在本例中这个参数为 `temp`

This use case of wanting to reuse the last argument of the last command (here `temp`) is so common that Bash stores it in a special variable `_`, which we access using a `$` prefix (same as for `$PATH` or `$HOME`).
想要重用上一个命令的最后一个参数(`temp`)的用例非常普遍,以至于 Bash 将其存储在一个特殊的变量 `_` 中。我们需要使用 `$` 前缀来访问它(与 `$PATH` `$HOME` 相同)。

Another example of using `$_`— moving a file and printing to `STDOUT` using `cat`:
使用 `$_` 的另一个示例 —— 移动文件并借助 `cat` 使用打印内容到 `STDOUT`

```bash
$ mv main.py src/main.py
$ cat src/main.py
```

Notice how we are again reusing the last argument `src/main.py`?
请注意,我们如何再次重用最后一个参数 `src/main.py`

You can rewrite this using `$_` to automatically bring forward `src/main.py` into your second command:
你可以使用以下命令重写此代码,`$_` 在第二个命令中会自动替换为 `src/main.py`

```bash
$ mv main.py src/main.py
$ cat $_
cat src/main.py
```

Using `$_` means you don't need to rewrite a complicated file path, giving you no chance to incorrectly retype it.
使用 `$_` 意味着你不需要重写复杂的文件路径,从而不会在重新键入时出错。

#### Summary
#### 小结

**Any time you are retyping something multiple times across multiple commands, it’s likely using** `$_` **can help reduce the strain on your weary hands.**
每当你需要在多个命令重复键入某些内容时,使用 `$_` 可能有助于减轻疲惫的双手的压力。

## Quick Substitution With ^old^new
## 使用 `^old^new` 快速替换

Sometimes (often in our case) we run a command in the Shell and quickly realize we made a mistake.
有时(在我们的情况下),我们在命令行管理程序中运行了一个命令,并很快意识到自己犯了一个错误。

Rather than retyping the command again, we can use **quick substitution to fix the mistake by replacing text in the previous command.**
但其实我们无需再次键入命令,我们可以使用快速替换,通过更换上一个命令中的内容来修复错误。

An example — you are SSHing into a server and run the command to connect — only to realise it should have been`user` instead of `ubuntu` all along!
举个例子 —— 你希望通过 ssh 连接到服务器,并运行了命令去连接 —— 运行后才意识到应该是 user 而非 ubuntu

```bash
$ ssh [email protected]
```

Instead of retyping the entire command again, you can use quick substitution to change just the part you want — here to change `ubuntu` into `user`:
你可以使用快速替换来更改所需的部分,而不必再次重新键入整个命令以在此处将 ubuntu 改为 user

```bash
$ ^ubuntu^user
ssh [email protected]
```

The pattern in quick substitution is `^old^new`. It is the equivalent of doing:
快速替换的格式是 `^old^new`,等效于:

```bash
$ !!:s/old/new
```

`!!` to get the last command and `:s` for a substitute regex. I think you'll agree `^old^new` is a little easier!
`!!` 用于获取最后一个命令,而 `:s` 是替换的正则表达式。我想你会同意 `^old^new` 减轻了不少工作负担!

#### Summary
#### 小结

**Any time you are retyping something multiple times across multiple commands, it’s likely using** `$_` **can help reduce the strain on your worn out hands.**
**每当你通过多个命令多次重新键入某些内容时,使用 `$_` 可能对减轻重复输入的压力有帮助。**

---

Thanks for reading!
感谢阅读!

> 如果发现译文存在错误或其他需要改进的地方,欢迎到 [掘金翻译计划](https://github.com/xitu/gold-miner) 对译文进行修改并 PR,也可获得相应奖励积分。文章开头的 **本文永久链接** 即为本文在 GitHub 上的 MarkDown 链接。
Expand Down

0 comments on commit 9b0049c

Please sign in to comment.