The basic features of bash

1. The command history
    1.1 history :Display or manipulate the history list.
        [root@promote tmp]# history    //Display the history list.
        -a: Append newly executed commands to the command history file(.bash_history).
            [root@bogon ~]# history -a
        -d: Delete specified command in the history list. (not in the command history file)
            [root@bogon ~]# history -d 2    //Delete the second command of the history list.
        -c: Clear the history list.(not the commands in the command history file) 
            [root@bogon ~]# history -c
        history # : Display the last N commands of the history list.
            [root@bogon ~]# history 5    //Display the last five commands of the history list.
    1.2 Environment variables related to command history.
        HISTSIZE : The number of entries in the command history.
        HISTFILE : Each user has his own command history file which is located in HOME_DIR and named .bash_history. 
            the full path of .bash_history was saved in HISTFILE.
        HISTFILESIZE : The number of entries in .bash_history.Experience carefully the difference between HISTSIZE and HISTFILESIZE.Here's the explanation
            When a user logs in to the shell,the shell will read the entries(条目) in .bash_history.
            When a user logs in to the shell,newly executed commands are only recorded in the cache and whill be appended to .bash_history when the user exits. 
    1.3 shortcuts
        !#: Executed Nth command of history list.
            [root@bogon ~]# !5  //Execute the fifth command in history list(not in .bash_history)
        !string:Execute the last command begins with "string";
        !!: Execute the previous command. 
            [root@bogon ~]# !!  //Execute the previous command
    1.4 Call the last parameter of the previous command.
        !$ : 
        ESC,. : Press the ESC key first,then release,then press the . key
    1.5 Control the way commands are recorded by setting the value of HISTCONTROL(a environment variables).
        HISTCONTROL
            ignoredups:Ignore repeated commands,continues and the same is repeated.
            ignorespace:Ignore commands begins with backspaces,so you can add a space before the command if you want the command not recorded
            ignoreboth:ignoredups, ignorespace;
        Changing the value of HISTCONTROL only works for the current shell process.The value will not change in a new terminnal.
            [root@bogon ~]# export HISTCONTROL="ignoredups"
2.command completion
    2.1 command:Executing command is running programs in essence.
        Internal command : We don't need to find it.
        External command : The bash will find the file named after the given command name according to the paths from left to right in PATH . 
    2.2 compelete directly :press Tab. The string given by the user has only one corresponding command. 
        If the string given by the user has multiple corresponding commands,pressing TAB again will print all corresponding commands.    
3.path compeletion :Consider the user-given string as the beginning of the path,and search the file named after the user-given string from the parent directory of the current directory.
    compelete directly : If the user-given string has only one corresponding file,the shell will compelete directly.    
    or pressing TAB again will print all the corresponding files. 
4.command line expansion
    ~: refers to user's home directory
    ~USERNAME:refers to specified user's home directory
    {}:with a Comma-separated list in the Curly braces,and the list will expand into multiple paths.
        /tmp/{a,b} = /tmp/a, /tmp/b
    /tmp/{tom,jerry}/hi = /tmp/tom/hi, /tmp/jerry/hi
    (1) How create /tmp/x/y1, /tmp/x/y2, /tmp/x/y1/a, /tmp/x/y1/b, /tmp/x/y2/a, /tmp/x/y2/b
    mkdir /tmp/x/{y1,y2}/{a,b}        
    (2) How create x_m, y_m, x_n, y_n
    mkdir {x,y}_{m,n}
    (3) How create /tmp/bin, /tmp/sbin, /tmp/usr, /tmp/usr/bin, /tmp/usr/sbin
    mkdir -p /tmp/{bin,sbin,usr/{bin,sbin}}    

5.The result of the execution of the command and the result of the execution state.
    5.1 the result of the execution state : sucess or fail
        bash saves the result of execution state of the last command in $? which is a specicial value.
            0 : refers to sucess.
            1 - 255 : refers to fail.
    5.2 the return value

6.命令别名:通过alias命令来管理命令别名
    6.1 alias : Define or display aliases.
        [root@bogon ~]# alias   //显示当前所有别名
        [root@bogon ~]# alias cdnet='cd /etc/sysconfig/network-scripts'  //定义一个别名
    6.2 在命令行中定义的别名,仅对当前shell进程有效;如果想永久有效,要定义在配置文件中;
        仅对当前用户:~/.bashrc
        对所有用户有效:/etc/bashrc
        Note: 编辑配置给出的新配置不会立即生效,需要重新读取编辑的配置文件;
            bash进程重新读取配置文件:
                source /path/to/config_file
                .  /path/to/config_file
    6.3 撤销别名
        [root@bogon ~]# unalias   //撤销所有别名
        [root@bogon ~]# alias cdnet  //撤销cdnet
    
    6.4 Note: 如果别名同原命令的名称,则如果要执行原命令,可使用"\COMMAND";
7.glob(globing)
    7.1 * : 任意长度的任意字符
    7.2 ? : 任意单个字符
    7.3 [] : 匹配指定范围内的任意单个字符
        [0-9] : 0-9之间的任意一个
        [a-z] : 不区分字符大小写
        [A-Z] : 这里只通配大写,上面的不区分大小写
    7.4 [^] : 匹配指定范围外的任意单个字符
        [^0-9]
        [^0-9a-z] : 任意的特殊字符
    7.5 专用字符集合
        [:digit:]:任意数字,相当于0-9  [[:digit:]]相当于[0-9]
        [:lower:]:任意小写字母
        [:upper:]: 任意大写字母
        [:alpha:]: 任意大小写字母
        [:alnum:]:任意数字或字母
        [:space:]:空格字符
        [:punct:]:标点符号
    7.5 实例
        1)、显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录;
            # ls -d /var/l*[0-9]*[[:lower:]]

        2)、显示/etc目录下,以任意一位数字开头,且以非数字结尾的文件或目录;
            # ls -d /etc/[0-9]*[^0-9]

        3)、显示/etc/目录下,以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录;
            # ls /etc/[^[:alpha:]][[:alpha:]]*

        4)、复制/etc目录下,所有以m开头,以非数字结尾的文件或目录至/tmp/mageedu目录中;
            # cp -a /etc/m*[^0-9] /tmp/mageedu

        5)、复制/etc目录下,所有以.d结尾的文件或目录至/tmp/magedu.com目录中;
            # cp -a /etc/*.d /tmp/magedu.com

        6)、复制/etc目录下,所以有.conf结尾,且以m,n,r,p开头的文件或目录至/tmp/mageedu.com目录中;
            # cp -a /etc/[mnrp]*.conf  /tmp/mageedu.com

猜你喜欢

转载自blog.csdn.net/liujun5319/article/details/82106576
今日推荐