history命令解析_学习笔记

简介: 时间:2017.11.13作者:李强参考:man,info,magedu讲义声明:以下英文纯属个人翻译,英文B级,欢迎纠正,盗版不纠,才能有限,希望不误人子弟为好。1、使用目的与场景    实现快速操作命令的一种方式2、官方说明    Display or manipulate the history list.3、写在前面    首先这里有两个概念history list和history file。

时间:2017.11.13

作者:李强

参考:man,info,magedu讲义

声明:以下英文纯属个人翻译,英文B级,欢迎纠正,盗版不纠,才能有限,希望不误人子弟为好。

1、使用目的与场景

    实现快速操作命令的一种方式

2、官方说明

    Display or manipulate the history list.

3、写在前面

    首先这里有两个概念history list和history file。下面会使用到,history list就是memory中的东西,history file就是disk上的东西。history list 针对当前会话生效,修改history file是针对所有用户生效。

当我们通过终端登陆系统后,就会自动将默认history file文件中的命令添加到history list中去,当然logout后history list中的命令就会自动写入到history file中去,就是存盘。流程图就不画了自己意会。

4、修改文件及涉及的环境变量

文件:

~/.bash_history 历史列表的默认保存位置
~/.bash_profile  当前用户的环境变量的设置位置
/etc/profile 系统环境变量的默认设置位置
 /etc/profile.d/env.sh (这个文件自定义): 手动添加的修改环境变量的设置位置

    系统是啥,系统就是"地主"想做啥做啥。

变量:

HISTSIZE

命令历史记录的条数

HISTFILE 指定历史文件,默认为~/.bash_history 
HISTFILESIZE 命令历史文件记录历史的条数 
HISTTIMEFORMAT HISTTIMEFORMAT="%F %T"   显示时间
HISTIGNORE HISTIGNORE="str1:str2*:… "忽略str1命令,str2开头的历史
HISTCONTROL ignoredups 默认,忽略重复的命令,连续且相同为“重复” 

ignorespace 忽略所有以空白开头的命令

ignoreboth 相当于ignoredups, ignorespace的组合 

erasedups 删除重复命令 

ps.设置的是history list的值,执行history我一次不想看太多命令

ps.和date的format是一样的。 

ps.可以设置忽略已空格开头的命令和下面的HISTCONTROL=ignorespace相同,在命令加空格就不会记录了;匹配是精确匹配,str1会忽略,但str1xx就会记录。

ps.可以用 : 隔开,配合设置,HISTCONTROL='ignorespace:ignoredups' 

export 变量名="值“ 存放在 /etc/profile 或 ~/.bash_profile



5、用法(不按字母排序)

history: history [-c] [-d offset] [n] or history -anrw [filename] 
                   or history -ps arg [arg...]
    #ps.官方,以下为个人解释。
    -c   清空当前history list缓存
    -d   删除list中的某个命令,后面跟的是number号,就是上面的offset
     n   显示最近n条命令
    -a   append 添加当前会话history list中的命令历史到文件中去
    -n   new 只添加history list中没有从history file中添加过的命令
    -r   read 读取history file中的文件到当前session的history file中
    -w   write 把当前history file中的命令覆盖到history文件中去。
    可以跟上另外的history file做备份,也可以配合-c先清空然后再默认不加[filename],
    清空~/.bash_history 文件。
    -p   执行但是不记录到history list中
    -s   会把参数添加到history list中去,但是并不执行

以上是option选项,以下还有一些bash对history list的快捷操作 History expansions


    The History library provides a history expansion feature that is similar to the history expansion provided by `csh'.  This section describes the syntax used to manipulate the history information.

    History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.

    History expansion takes place in two parts.  The first is todetermine which line from the history list should be used during substitution.  The second is to select portions of that line for inclusion into the current one.  The line selected from the history iscalled the "event", and the portions of that line that are acted upon are called "words".  Various "modifiers" are available to manipulate the selected words.  The line is broken into words in the same fashion that Bash does, so that several words surrounded by quotes are considered one word.  

    History expansions are introduced by the appearance of the history expansion character, which is `!' by default

以下为本人翻译,凑活着看:

    history调用将history list 列表中的单词引入输入流, 使重复命令变得容易, 将参数插入到当前输入行中的上一个命令, 或快速修复以前命令中的错误。

    history调用分成2个部分,第一我们要明确我们在置换的时候选择的是history list的哪一行,第二选择这行的哪个部分去加入到当前命令中。 行的选择我们称为”event“,选择这行的哪一部分称为‘’words‘’

   

history的调用默认用!字符引入调用
执行操作格式如下:
   [command]  !xx:yy   !xx哪一行命令,中间":"分开,yy 第几个参数
event Designators标识符,以下指定哪一行被调用。
`!'     :开始history调用,除非后面跟spasce,tab,或者`='在后面,就不是这个意思了,
#ps.后面要直接加东西的意思,如下面所示必要手残多敲些东西的意思
`!N'     :执行第N行命令
`!-N'    :执行倒数第N行命令
`!!'     :执行刚才或者上一个执行的命令,相当于 `!-1'  
`!STRING'  :从最后一个往前,执行最近的一个以STRING开头的命令
`!?STRING[?]':从最后一个往前,执行最近的一个包含STRING开头的命令
#这里有个快捷键实现上面2个方式,Ctrl+r向上查找最近的一个以某个字符开头或
包含某个字符的命令回车执行,Ctrl +g 取消搜索
`^STRING`:替换上一条命令的第一个STRING
`^STRING1^STRING2^':快速替换,将上条命令中的STRING1替换成STRING2
!!:$'    :标识前一个命令的最后一个参数可以缩写成 `!$'
`!fi:2'   :表示最近一个以fi开始命令的第二个参数,表示不懂
#以下是一些word Designators标识符
`0 (zero)' :第0个单词,对于大部分应用来说是命令本身
#ps. "!:0" 执行前一个命令但是不加参数。
`N'     :第N个单词
`^'     :第一个参数,也是第一个单词
`$'     :最后一个参数
`%'
The word matched by the most recent `?STRING?' search.
`X-Y'   一个单词的范围,`-Y' 是 `0-Y'的缩写
`*'     :所有的单词,除了第0个也就是命令本身和`1-$'是相同的意思
`X*'     :`X-$'的缩写
`X-
modify
After the optional word designator, you can add a sequence of one or
more of the following modifiers, each preceded by a `:'.
    
`h'     :移除尾部的路径,保留路径的头
`t'     :移除路径的头部,保留尾,
e.g.
[lq@centos6 ~]$ll /etc/passwd /etc/motd
-rw-r--r--. 1 root root    0 Jan 12  2010 /etc/motd
-rw-r--r--. 1 root root 1920 Nov 15 19:02 /etc/passwd
[lq@centos6 ~]$echo "!!:t"
echo "motd"
motd
`r'     :移除`.SUFFIX'后缀,保留文件名
e.g.
[root@centos6 /app]#ll  1.txt 2.txt        
-rw-r--r--. 1 root root 0 Nov 15 22:27 1.txt
-rw-r--r--. 1 root root 0 Nov 15 22:27 2.txt
[root@centos6 /app]#echo "!!:r"            
echo "ll  1.txt 2"
ll  1.txt 2
`e'     :移除所有,保留`.SUFFIX'后缀
e.g.
[root@centos6 /app]#ll  1.txt 2.txt    
-rw-r--r--. 1 root root 0 Nov 15 22:27 1.txt
-rw-r--r--. 1 root root 0 Nov 15 22:27 2.txt
[root@centos6 /app]#echo "!!:e"
echo ".txt "
.txt 
`p'     :打印命令但是不执行
e.g.
[root@centos6 /app]#!518:p
rm -rf *
[root@centos6 /app]#!-10:p
ll /etc/passwd 
`s/OLD/NEW/'
     Substitute NEW for the first occurrence of OLD in the event line.
     Any delimiter may be used in place of `/'.  The delimiter may be
     quoted in OLD and NEW with a single backslash.  If `&' appears in
     NEW, it is replaced by OLD.  A single backslash will quote the
     `&'.  The final delimiter is optional if it is the last character
     on the input line.     
`&'        :重复之前的替换,暂时没找到使用方法
`g'
#ps.虽然看不懂但是是怎么用的 
#!:gs/string1/string2 将上一条命令中所有的string1都替换为string2
`a'
     Cause changes to be applied over the entire event line.  Used in
     conjunction with `s', as in `gs/OLD/NEW/', or with `&'.
     `G'
     Apply the following `s' modifier once to each word in the event.

6、实际使用:

    command !^     : 利用上一个命令的第一个参数做cmd的参数 

    command !$      : 利用上一个命令的最后一个参数做cmd的参数 

    command !*      : 利用上一个命令的全部参数做cmd的参数 

    command !:n     : 利用上一个命令的第n个参数做cmd的参数

    ps. !^  可以理解为"!-1:^"或者"!!:^"的缩写

         !$! 可以理解为"!-1:$"或者"!!:$"的缩写

         !*! 可以理解为"!-1:*"  或者"!!:*"的缩写

        这里就可以理解了!!在加上:之后可以缩写为!,这就是之前的!:0代表上一个命令的意思。

        下面的就好理解了!xx:yy

    command !n:^      调用第n条命令的第一个参数 

    command !n:$      调用第n条命令的最后一个参数 

    command !n:n     调用第n条命令的第n个参数 

    command !n:*       调用第n条命令的所有参数

    command !string:^     从命令历史中搜索以 string 开头的命令 ,并获取它的第一个参数 

    command !string:$     从命令历史中搜索以 string 开头的命令 ,并获取它的最后一个参数

    command !string:n     从命令历史中搜索以 string 开头的命令 ,并获取它的第n个参数 

    command !string:*     从命令历史中搜索以 string 开头的命令 ,并获取它的所有参数

    

凑合看吧,顺便说下我不是喜欢绿色,我喜欢的是大自然的颜色

j_0003.gif

相关文章
|
6月前
|
Linux
Linux命令之history
Linux命令 history
125 0
|
7月前
|
Arthas JSON Java
Arthas中help、history、keymap、options基础命令应用
通过本教程的操作,您可以体验如何Alibaba Cloud Linux  2.1903 LTS 64位操作系统的云服务器上学习Help、history、keymap、options基础命令教程用法。
89 0
|
存储 Linux 应用服务中间件
history的操作你知道几个
history的操作你知道几个
111 0
history的操作你知道几个
Confluence 6.15 修改历史(Change-History)宏
修改历史(Change-History)宏显示了页面一个的更新历史:版本号,作者,日期和备注。这些内容将会在同一栏中进行显示。 屏幕截图:Confluence 中的修改历史(Change-History)宏 为了向页面中插入修改历史宏:  从编辑工具栏中,选择 插入(Insert)   > 其他宏(Other Macros)找到并且选择需要的宏。
774 0
|
Perl 开发工具