“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022”
- 3 Uncommon Bash Tricks That You Should Know
- By Adam Green
- The Nuggets translation Project
Good developers tend to type less — which also allows them to:
- Work faster
- Work more accurately
- Reduce errors
- Relieve the pressure of tired hands
One way to reduce typing is to use your command line (usually Bash) properly, and this article will show you three Bash tips for reducing typing.
The three Bash techniques covered in this article are:
- use
{a,b}
— Extend the parameters to avoid typing a single command again - use
The $_
Access the last parameter to avoid retyping from the last command - use
^old^new
— Quickly change part of the last command
All of these techniques are compatible with ZSH.
The commands entered in this article are as follows$
At the beginning. For the code parsed from the command line, I will leave it blank under the relevant code$
Write the parsed code equivalent to this line of code.
{a,b}
Expand the parameters
When we write commands, we often type the same command repeatedly.
For example, to change the file suffix, we usually use mv:
$ mv README.txt README.md
Copy the code
Notice that we wrote the README twice, and the parameter extension avoids this duplication by changing the file’s suffix without typing the README twice:
$ mv README.{txt,md}
mv README.txt README.md
Copy the code
The argument we are using is expanded to {TXT,md}, and it will be expanded to two arguments — TXT md (separated by Spaces).
Parameter extensions create one parameter for each element enclosed in curly braces separated by commas:
$ echo {1,2,3}
1 2 3
$ echoThe pre {1, 2, 3} pre1 pre2 pre3Copy the code
The empty element creates a parameter without any substitution:
$ echoThe pre, {1, 2} fix the prefix pre1fix pre2fixCopy the code
Another example — we’ll rename the Models folder in the data folder to ML:
$ mv data/models data/ml
Copy the code
We can use parameter extensions to save time retyping data/ :
$ mv data/{models,ml}
mv data/models data/ml
Copy the code
We can use parameter extensions with numeric sequences — useful when creating sequentially numbered directories:
$ mkdir data{0.. 2} mkdir data0 data1 data2Copy the code
We can also extend parameters within parameters — for example, by changing the folder name in the path:
$ cat models/{baseline,final}/data.csv
cat models/baseline/data.csv models/final/data.csv
Copy the code
One final example: in the mv command, we use three arguments — to move two Python files to the Tests folder:
$ mv test_unit.py test_system.py tests
Copy the code
summary
Parameter extensions can greatly help you save effort whenever you type something multiple times in a single command.
useThe $_
Use access to the last parameter
A terminal consists of a series of command operations, and we often reuse information between multiple commands.
If our previous technique, parameter extension, is used to reduce typing on a single command. Now this technique is used to reduce typing of multiple commands.
Take the case of creating a folder and moving the current directory to it:
$ mkdir temp
$ cd temp
Copy the code
Now we can actually avoid typing the same thing over and over again by passing the arguments to the previous command with $_ :
$ mkdir temp
$ cd The $_
cd temp
Copy the code
In the above code, we use $_ to access the last parameter of the previous command, which in this case is temp.
Situations where you want to reuse the last argument of a previous command (temp in this case) are actually quite common, so much so that Bash stores it in a special variable _. We need to access it with the $prefix (same as $PATH or $HOME).
Here’s another example of using $_ — moving a file and using print to STDOUT with cat:
$ mv main.py src/main.py
$ cat src/main.py
Copy the code
So how do we reuse the last parameter SRC /main.py again?
You can override this code with the following command, $_ is automatically replaced with SRC /main.py in the second command:
$ mv main.py src/main.py
$ cat The $_
cat src/main.py
Copy the code
Using $_ means you don’t have to rewrite complex file paths so you don’t have to retype errors.
summary
Use this whenever you need to type something repeatedly in multiple commandsThe $_
May help relieve pressure on tired hands.
use^old^new
Quick replace
Sometimes we might run a command in a command-line hypervisor and quickly realize that we made an error in the command.
But instead of typing the command again, we can use quick replace to fix the error by replacing the previous command.
For example — you want to connect to the server over SSH, and you run the command to do it — and then realize that the username should be User and not Ubuntu!
$ ssh [email protected]
Copy the code
You can use quick substitution to change the required parts without having to retype the entire command again to change Ubuntu to user here:
$ ^ubuntu^user
ssh [email protected]
Copy the code
A quick replacement is ^old^new, equivalent to:
$!!!!! :s/old/newCopy the code
!!!!! Is used to get the last command, and :s is the replacement regular expression. I think you’ll agree that it’s quite a relief!
summary
When you write a wrong command and the command is not easy to rewrite (e.g., very long), then use^old^new
Can greatly reduce your problems.
Thanks for reading!
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.