bash特性

bash的基础特性之一:

         命令历史:shell进程会其会话中保存此前用户提交执行过的命令;

                   ~]#history

                            定制history的功能,可通过环境白能量实现;

                                     HISTSIZE:shell进程可保留的命令历史的条数

                                     HSITFILE:持久保存命令历史的文件

                                               .bash_history

                                     HISTFIELSIZE:命令历史文件的大小;

                   命令用法:

                            history[-c] [-d 偏移量] [n]

                            history �anrw[文件名]

                            history �ps 参数 [参数…]

                           

                            -c:清空命令历史;

                            -doffset:删除指定命令历史

                            -r:从文件读取命令历史之历史列表中;

                            -w:把历史列表中的命令追加至历史文件中;

                            history#:显示最近的#条命令;

调用命令历史列表中的命令

                            !#:再一次执行历史列表中的第#条命令;

                            !!:再一次执行上一条命令;

                            !STRING:再一次执行命令历史列表中最近一个以STRING开头的命令;

                                     注意:命令的重复执行有时候需要依赖于幂等性;                           

                   调用上一条命令的最后一个参数:

                            快捷键:Esc, .

                            字符串:!$

                 控制命令历史记录的方式;

                            环境变量:HISTCONFTROL

                                     ignoredups:忽略重复的命令;

                                     ignorespace:忽略以空白字符开头的命令;

                                     ignoreboth:以上两者同时生效;

                            修改变量的值:

                                     NAME=’VALUE’

Bash基础特性之二:

 

                   命令补全:

                            shell程序在接收到用户执行命令的请求,分析完成之后,最左侧的字符串会被当作命令;

                            命令查找机制:

                                     查找内部命令;

                                     根据PATH环境变量中设定的目录,自左而右逐个搜索目录下的文件名;

 

                            给定的打头字符串如果能惟一标识某命令程序文件,则直接补全;

                                                不能惟一标识某命令程序文件,再击tab键一次,会给出列表;

 

                   路径补全:

                            在给定的起始路径下,以对应路径下的打头字串来逐一匹配起始路径下的每个文件:

                                     tab

                                               如果能惟一标识,则直接补全;

                                               否则,再一次tab,给出列表;

bash的基础特性之三:命令行展开

                   ~:自动展开为用户的家目录,或指定的用户的家目录;                         

                   {}:可承载一个以逗号分隔的路径列表,并能够将其展开为多个路径;

                            例如:/tmp/{a,b}相当于 /tmp/a /tmp/b

bash的基础特性之四:命令的执行状态结果

 

                   命令执行的状态结果:

                            bash通过状态返回值来输出此结果:

                                     成功:0

                                     失败:1-255

 

                            命令执行完成之后,其状态返回值保存于bash的特殊变量$?中;

 

                   命令正常执行时,有的还回有命令返回值:

                            根据命令及其功能不同,结果各不相同;

 

                   引用命令的执行结果:

                            $(COMMAND)

                            `COMMAND`

bash的基于特性之五:引用

                   强引用:''

                   弱引用:""

                   命令引用:``

bash基础特性之六:快捷键

                   Ctrl+a:跳转至命令行行首

                   Ctrl+e:跳转至命令行行尾

 

                   Ctrl+u:删除行首至光标所在处之间的所有字符;

                   Ctrl+k:删除光标所在处至行尾的所有字符;

 

                   Ctrl+l:清屏,相当于clear

bash的基础特性之七:

 

         globbing:文件名通配(整体文件名匹配,而非部分)

 

                   匹配模式:元字符

                            *:匹配任意长度的任意字符

                                     pa*,*pa*, *pa, *p*a*

                                               pa,paa, passwd

                            ?:匹配任意单个字符

                                     pa?,??pa, p?a, p?a?

                                               pa,paa, passwd

                            []:匹配指定范围内的任意单个字符

                                     有几种特殊格式:

                                               [a-z],[A-Z], [0-9], [a-z0-9]

                                               [[:upper:]]:所有大写字母

                                               [[:lower:]]:所有小写字母

                                               [[:alpha:]]:所有字母

                                               [[:digit:]]:所有数字

                                               [[:alnum:]]:所有的字母和数字

                                               [[:space:]]:所有空白字符

                                               [[:punct:]]:所有标点符号

 

                                               pa[0-9][0-9],2[0-9][0-9]

                            [^]:匹配指定范围外的任意单个字符

                                     [^[:upper:]]

                                     [^0-9]

                                     [^[:alnum:]]

bash特性之:命令hash

                   缓存此前命令的查找结果:key-value

                            key:搜索键

                            value:值

 

                   hash命令:

                            hash:列出

                            hash-d COMMAND:删除

                            hash-r:清空

 

    bash的特性之:变量

                   程序:指令+数据

                            指令:由程序文件提供;

                            数据:IO设备、文件、管道、变量

 

                   程序:算法+数据结构

 

                   变量名+指向的内存空间

 

                   变量赋值:name=value

                   变量类型:存储格式、表示数据范围、参与的运算

                            编程语言:

                                     强类型变量

                                     弱类型变量:

                                               bash把所有变量统统视作字符型;

                                               bash中的变量无需事先声明;相当于,把声明和赋值过程同时实现;

                                                        声明:类型,变量名

                   变量替换:把变量名出现的位置替换为其所指向的内存空间中数据;

                   变量引用:${var_name},$var_name

                   变量名:变量名只能包含数字、字母和下划线,而且不能以数字开头;

                            变量名:见名知义,命名机制遵循某种法则;不能够使用程序的保留字,例如if, else, then, while等等;

                                    

                   bash变量类型:

                            本地变量:作用域仅为当前shell进程;

                            环境变量:作用域为当前shell进程及其子进程;

                            局部变量:作用域仅为某代码片断(函数上下文)

 

                            位置参数变量:当执行脚本的shell进程传递的参数;

                            特殊变量:shell内置的有特殊功用的变量;

                                     $?

                                               0:成功

                                               1-255:失败

 

                            本地变量:

                                     变量赋值:name=value

                                     变量引用:${name},$name

                                               "":变量名会替换为其值;

                                               '':变量名不会替换为其值;

                                     查看变量:set

                                     撤销变量:unsetname

                                               注意:此处非变量引用;

 

                            环境变量:

                                     变量赋值:

                                               (1)export name=value

                                               (2)name=value

                                                   export name

                                               (3)declare -x name=value

                                               (4)name=value

                                                        declare-x name

                                     变量引用:${name},$name

 

                                     注意:bash内嵌了许多环境变量(通常为全大写字符),用于定义bash的工作环境

                                               PATH,HISTFILE, HISTSIZE, HISTFILESIZE, HISTCONTROL, SHELL, HOME, UID, PWD, OLDPWD

 

                                     查看环境变量:export,declare -x, printenv, env

                                     撤销环境变量:unsetname

 

                            只读变量:

                                     (1)declare -r name

                                     (2)readonly name

 

                                     只读变量无法重新赋值,并且不支持撤销;存活时间为当前shell进程的生命周期,随shell进程终止而终止;

bash特性之多命令执行:

                   ~]#COMMAND1; COMMAND2; COMMAND3; ...

 

                   逻辑运算:

                            运算数:真(true,yes, on, 1)

                                         (false, no, off, 0)

 

                            与:

                                     1&& 1 = 1

                                     1&& 0 = 0

                                     0&& 1 = 0

                                     0&& 0 = 0

                            或:

                                     1|| 1 = 1

                                     1|| 0 = 1

                                     0|| 1 = 1

                                     0|| 0 = 0

                            非:

                                     !1 = 0

                                     !0 = 1

 

                   短路法则:

                            ~]#COMMAND1 && COMMAND2

                                     COMMAND1为“假”,则COMMAND2不会再执行;

                                     否则,COMMAND1为“真”,则COMMAND2必须执行;

 

                            ~]#COMMAND1 || COMMAND2

                                     COMMAND1为“真”,则COMMAND2不会再执行;

                                     否则,COMMAND1为“假”,则COMMAND2必须执行;

 

                            示例:~]# id$username || useradd $username

 


你可能感兴趣的:(bash,特性)