当前位置:网站首页>The necessary operation for those big guys to fly 666 inadvertently at the bash command line terminal
The necessary operation for those big guys to fly 666 inadvertently at the bash command line terminal
2022-06-29 11:08:00 【ghostwritten】
shell Command line operations
List of articles
–
brief introduction
Bash Built in Readline library , This library provides many “ Line operation ” function , For example, automatic completion of commands , It can greatly speed up the operation .
This library defaults to Emacs Shortcut key , It can also be changed to Vi Shortcut key .
$ set -o vi
The following command can be changed back to Emacs Shortcut key .
$ set -o emacs
If you want to permanently change the editing mode (Emacs / Vi), You can write the command in ~/.inputrc file , This file is Readline Configuration file for .
set editing-mode vi
The shortcuts described in this chapter belong to Emacs Pattern .Vi Mode shortcut key , Readers can refer to Vi Editor tutorial .
Bash This library is enabled by default , But closing is allowed .
$ bash --noediting
In the above command ,--noediting Parameter closed Readline library , Starting up Bash There is no line operation function .
2. Cursor movement
Readline Provides shortcut keys for quickly moving the cursor .
Ctrl + a: Moved to the beginning of a line .Ctrl + b: Move a character to the beginning of the line , Same as the left arrow .Ctrl + e: Moved to the end of each line .Ctrl + f: Move a character to the end of the line , Same as the right arrow .Alt + f: Move to the end of the current word .Alt + b: Move to the beginning of the current word .
The shortcut keys above Alt key , It can also be used. ESC Key instead of .
3. Clear the screen
Ctrl + l The shortcut key can clear the screen , Moves the current line to the first line of the screen , And clear Orders work the same .
4. Edit operation
The following shortcut keys can edit the contents of the command line .
Ctrl + d: Delete character at cursor position (delete).Ctrl + w: Delete the word in front of the cursor .Ctrl + t: The character at the cursor position is exchanged with the character in front of it (transpose).Alt + t: The word at the cursor position is exchanged with the word in front of it (transpose).Alt + l: Change the cursor position to lowercase at the end of the word (lowercase).Alt + u: Change the cursor position to uppercase at the end of the word (uppercase).
[!NOTE|style:flat|lable:Mylable|iconVisibility:hidden]
UseCtrl + dWhen , If there are no characters in the current line , Will lead to exit from the current Shell, So be careful .
The shortcut keys for cutting and pasting are as follows .
Ctrl + k: Cut the text from the cursor position to the end of the line .Ctrl + u: Cut the text where the cursor is positioned to the beginning of the line .Alt + d: Cut the text from the cursor position to the end of the word .Alt + Backspace: Cut the text from the cursor position to the beginning of the word .Ctrl + y: Paste text at the cursor position .
similarly ,Alt Key can be used Esc Key instead of .
5. Automatic completion
Halfway through the command , You can press Tab key ,Readline Will automatically complete the command or path . such as , Input cle, Then press the Tab key ,Bash This command will be automatically completed as clear.
If there are more than one qualified command or path , You need to press it twice in a row Tab key ,Bash All qualified commands or paths will be prompted .
Except for commands or paths ,Tab You can also complete other values . If a value with $ start , Press Tab The key will complete the variable ; If the ~ start , Then complete the user name ; If the @ start , Then complete the host name (hostname), Host names are listed in /etc/hosts The host in the document shall prevail .
The shortcut keys related to automatic completion are as follows .
Tab: Complete auto completion .Alt + ?: List possible completions , And double click Tab Same bond action .Alt + /: Try file path completion .Ctrl + x /: According to the first Ctrl + x, Press again /, Equate to Alt + ?, List possible file paths to complete .Alt + !: Command Completion .Ctrl + x !: According to the first Ctrl + x, Press again !, Equate to Alt + !, Command Completion .Alt + ~: Complete the user name .Ctrl + x ~: According to the first Ctrl + x, Press again ~, Equate to Alt + ~, Complete the user name .Alt + $: Variable name completion .Ctrl + x $: According to the first Ctrl + x, Press again $, Equate to Alt + $, Variable name completion .Alt + @: Complete the host name .Ctrl + x @: According to the first Ctrl + x, Press again @, Equate to Alt + @, Complete the host name .Alt + *: Insert all possible completions at once on the command line .Alt + Tab: Try to use .bash_history It used to execute commands , To complete .
above Alt Keys can also be used ESC Key instead of .
6. Operation history
6.1 Basic usage
Bash The user's operation history will be preserved , That is, every command entered by the user will be recorded . With the operation history , You can use the arrow keys ↑ and ↓, Quickly browse the previous and next command .
Quit current Shell When ,Bash Will put the user in the current Shell Write the operation history of ~/.bash_history file , The file is stored by default 500 Operations .
environment variable HISTFILE Always point to this file .
$ echo $HISTFILE
/home/me/.bash_history
history The command will output the entire contents of this file . The user can see all the recently executed commands , Each command is preceded by a line number . The closer the command , Behind the other side .
$ history
...
498 echo Goodbye
499 ls ~
500 cd
When entering a command , Press down Ctrl + r Shortcut key , You can search the operation history , Select a previously executed command . At this point, type the beginning of the command ,Shell It will be automatically in the history file , Query and display the last matching result , Then press enter , Will execute that order .
The following method can quickly execute the previously executed commands .
$ echo Hello World
Hello World
$ echo Goodbye
Goodbye
$ !e
echo Goodbye
Goodbye
In the example above ,!e To find out the history of operations , The latest one is e The first command and execute .Bash Which command will be output first echo Goodbye, And then directly execute .
Empathy ,!echo We will also implement the latest one to echo The first order .
$ !echo
echo Goodbye
Goodbye
$ !echo H
echo Goodbye H
Goodbye H
$ !echo H G
echo Goodbye H G
Goodbye H G
Be careful ,!string Syntax only matches commands , It doesn't match the parameters . therefore !echo H Not execute echo Hello World, It's about executing echo Goodbye, And put the parameters H Attached to this order . Empathy ,!echo H G It's also equivalent to echo Goodbye Add... After the command H G.
because !string The syntax will be expanded to include previously executed commands , So it contains ! String in double quotation marks , You have to be very careful , If it is followed by a non space character , It is very likely to report an error .
$ echo "I say:\"hello!\""
bash: !\: event not found
The above command will report an error , The reason is that there is a backslash after the exclamation point ,Bash Will try to find , Have you ever executed a command that begins with a backslash before , If it is not found, an error will be reported . The solution is to precede the exclamation point , Plus a backslash .
$ echo "I say:\"hello\!\""
I say:"hello\!"
6.2 history command
As I said before ,history The command can display the operation history , namely .bash_history The content of the document .
$ history
Use this command , Instead of reading directly .bash_history The advantage of the document is , It will precede all operations with line numbers , The most recent operation is at the end , Maximum line number .
By customizing the environment variables HISTTIMEFORMAT, The time of each operation can be displayed .
$ export HISTTIMEFORMAT='%F %T '
$ history
1 2013-06-09 10:40:12 cat /etc/issue
2 2013-06-09 10:40:12 clear
In the above code ,%F amount to %Y - %m - %d,%T amount to %H : %M : %S.
Just set HISTTIMEFORMAT This environment variable , Will be in .bash_history The execution time stamp of the File Save command . If not set , The timestamp will not be saved .
environment variable HISTSIZE Set the number of save history operations .
$ export HISTSIZE=10000
The above command settings are saved in the past 10000 Operation history .
If you don't want to save the history of this operation , You can set HISTSIZE be equal to 0.
export HISTSIZE=0
If HISTSIZE=0 Write to the user's home directory ~/.bashrc file , Then the user's operation history will not be preserved . If written /etc/profile, The whole system will not retain operation history .
environment variable HISTIGNORE You can set which commands are not written to the operation history .
export HISTIGNORE='pwd:ls:exit'
The above example sets ,pwd、ls、exit These three commands do not write operation history .
If you want to search for a previously executed command , Can cooperate with grep Command search operation history .
$ history | grep /usr/bin
The above command returns .bash_history In the document , Those contain /usr/bin The order of .
Each record of the operation history has a number . After knowing the command number , You can use exclamation marks + Number to execute the command . If you want to execute .bash_history The inside number 8 Bar command , You can do this as follows .
$ !8
history Ordered -c Parameter can clear the operation history .
$ history -c
7. Related shortcuts
Here are some shortcut keys related to operation history .
Ctrl + p: Displays the last command , Same effect as the up arrow (previous).Ctrl + n: Show the next command , Same effect as the down arrow (next).Alt + <: Display the first command .Alt + >: Show the last command , The current command .Ctrl + o: Execute the current entry in the history file , And automatically display the next command . This is useful for repeating a sequence of commands .
Exclamatory mark ! The shortcut keys are as follows .
!!: Execute the last order .!n:n Is the number , The line number in the execution history file is n The order of .!-n: Before executing the current command n A command of .!string: Execute the latest to specify the string string The first order .!?string: Execute the last containing string string The order of .!$: Represents the last parameter of the previous command .!*: Represents all parameters of the previous command , That is, all parts except the command .^string1^string2: Execute the latest entry that contains string1 The order of , Replace it with string2.
Here is !$ and !* Example .
$ cp a.txt b.txt
$ echo !$
b.txt
$ cp a.txt b.txt
$ echo !*
a.txt b.txt
In the above example ,!$ Represents the last parameter of the previous command (b.txt),!* Represents all parameters of the previous command (a.txt b.txt).
Here is ^string1^string2 Example .
$ rm /var/log/httpd/error.log
$ ^error^access
rm /var/log/httpd/access.log
In the above example ,^error^access Put the latest one containing error In the order of error, Replace with access.
If you want to determine what the command is , And then execute , Can open histverify Options . In this case , Use ! Commands generated by shortcut keys , It will print out first , Wait until the user presses the Enter key .
$ shopt -s histverify
Other shortcuts
Ctrl + j: Equivalent to enter (LINEFEED).Ctrl + m: Equivalent to enter (CARRIAGE RETURN).Ctrl + o: Equivalent to enter , And show the next command in the operation history .Ctrl + v: Change the next special character to a literal , For example, carriage return becomes ^M.Ctrl + [: Equate to ESC.Alt + .: Insert the last word of the previous command .Alt + _: Equate to Alt + ..
above Alt + . Shortcut key , For very long file paths , Sometimes it's very convenient . because Unix The last argument to the command is usually the file path .
$ mkdir foo_bar
$ cd # Press down Alt + .
In the example above , stay cd Press... After the command Alt + ., Will be automatically inserted foo_bar.
Reference resources :
边栏推荐
- 什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手
- Please tell me about the Flink SQL batch task, two or more tables join (inner join or outer join
- How to obtain method parameter values through WinDbg
- 任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
- 5.移植uboot-设置默认环境变量,裁剪,并分区
- Online sql to htmltable tool
- (JS)迭代器模式
- Reids设计与实现
- arcgis创建postgre企业级数据库
- Numeric Keypad
猜你喜欢

Dormitory maintenance management system based on stm32+rfid design

CS231n-2022 Module1: 神经网络要点概述(2)

Mongodb tutorial Chapter 02 mongodb installation

【数字信号调制】基于 AM+FM+DSB+SSB实现信号调制解调含Matlab源码

加密市场接连爆雷,Celsius能避免破产吗?

BS-GX-018 基于SSM实现在校学生考试系统

What happened during the MySQL installation?

《Datawhale推荐系统教程》来了!

活动邀请 | Apache Doris 社区征文&演讲征集活动开始了!

UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. note
随机推荐
Exemples de programmation stm32f1 et stm32cubeide - entraînement du capteur de portée ultrasonique
Limit introduction summary
Qt编写物联网管理平台37-逻辑设计
flink sql cdc 并行度 会不会影响顺序呀,是不是数据同步的话一般只能设置1。
Excel date and number format processing
TTL串口学习型红外遥控模块可扩展成网络控制
在线文本过滤小于指定长度工具
(JS)筛选出对象中value大于2的key
Mongodb tutorial Chapter 02 mongodb installation
各位大佬 请教下mysqlcdc的数据不支持开窗函数吗 像row_number ,lead这种
Shell 中你不得不熟知的变量运用
map合并相同的键,值合并为列表
“AI x 科学计算”进行时,华为昇思 MindSpore 赛题火热开启,等你来!
Spark - one to one correspondence between task and partition and detailed explanation of parameters
8年打磨,《游戏设计梦工厂》发布史诗级更新!
在编写shell脚本时如何正确姿势地管理临时文件
Reids设计与实现
nuc980 已成功启动
np. astype()
(JS)捕获错误(异常)