Be Playful in Bash
HISTORY EXPANSION
Event Designators
An event designator is a reference to a command line entry in thehistory list.
- !
- Start a history substitution, except when followed by ablank,newline, carriage return, =or ( (when the extglob shell option is enabled usingthe shopt builtin).
- !n
- Refer to command linen.
- !-n
- Refer to the current command line minusn.
- !!
- Refer to the previous command. This is a synonym for `!-1'.
- !string
- Refer to the most recent command starting with string.
- !?string[?]
- Refer to the most recent command containingstring.The trailing ? may be omitted ifstringis followed immediately by a newline.
- ^string1^string2^
- Quick substitution. Repeat the last command, replacingstring1withstring2.Equivalent to``!!:s/string1/string2/''(see Modifiers below).
- !#
- The entire command line typed so far.
Word Designators
Word designators are used to select desired words from the event.A :separates the event specification from the word designator.It may be omitted if the word designator begins with a^,$,*,-,or%.Words are numbered from the beginning of the line,with the first word being denoted by 0 (zero).Words are inserted into the current line separated by single spaces.
- 0 (zero)
- The zeroth word. For the shell, this is the commandword.
- n
- The nth word.
- ^
- The first argument. That is, word 1.
- $
- The last argument.
- %
- The word matched by the most recent `?string?' search.
- x-y
- A range of words; `-y' abbreviates `0-y'.
- *
- All of the words but the zeroth. This is a synonymfor `1-$'. It is not an error to use*if there is just oneword in the event; the empty string is returned in that case.
- x*
- Abbreviates x-$.
- x-
- Abbreviates x-$ like x*, but omits the last word.
If a word designator is supplied without an event specification, theprevious command is used as the event.
Modifiers
After the optional word designator, there may appear a sequence ofone or more of the following modifiers, each preceded by a `:'.
- h
- Remove a trailing file name component, leaving only the head.
- t
- Remove all leading file name components, leaving the tail.
- r
- Remove a trailing suffix of the form .xxx, leaving thebasename.
- e
- Remove all but the trailing suffix.
- p
- Print the new command but do not execute it.
- q
- Quote the substituted words, escaping further substitutions.
- x
- Quote the substituted words as withq,but break into words atblanksand newlines.
- s/old/new/
- Substitutenewfor the first occurrence ofoldin the event line. Any delimiter can be used in place of /. Thefinal delimiter is optional if it is the last character of theevent line. The delimiter may be quoted inoldandnewwith a single backslash. If & appears innew,it is replaced byold.A single backslash will quote the &. Ifoldis null, it is set to the lastoldsubstituted, or, if no previous history substitutions took place,the laststringin a!?string[?]search.
- &
- Repeat the previous substitution.
- g
- Cause changes to be applied over the entire event line. This isused in conjunction with `:s' (e.g., `:gs/old/new/')or `:&'. If used with`:s', any delimiter can be usedin place of /, and the final delimiter is optionalif it is the last character of the event line.An a may be used as a synonym for g.
- G
- Apply the following `s' modifier once to each word in the event line.
posted on 2010-09-13 15:25 David Young 杨博华 阅读(333) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述