标签:family 单词 sys documents system video mtools size service
shell简介:
shell是核心程序kernel之外的指令解析器,是一个程序,同时是一种命令语言和程序设计语言。
shell是命令解析器,用户输入命令,它去解析,然后交给内核执行命令,内核便可以操作硬件。
变量:
命名规则:
以字母或者下划线开头,剩下的内容可以是字母、数字、下划线。
变量赋值:
[变量名]=[变量值]
echo $[变量名] //显示变量值
unset [变量名] //删除变量
export定义的变量和普通变量的区别:
注意:子bash中的export变量,不会被父bash继承。父bash中的export定义的变量会被子bash继承。
[root@centos-3 ~]# name=ldsly //普通方式定义变量 [root@centos-3 ~]# echo $name //在父bash中显示变量 ldsly [root@centos-3 ~]# bash //重新打开一个子bash [root@centos-3 ~]# echo $name //显示变量,发现并不存在这个变量 [root@centos-3 ~]# exit //退回父bash exit [root@centos-3 ~]# export name=ldsly //使用export定义变量 [root@centos-3 ~]# echo $name //在父bash中显示变量 ldsly [root@centos-3 ~]# bash //打开一个子bash [root@centos-3 ~]# echo $name //在子bash中显示变量,正常显示 ldsly [root@centos-3 ~]#
[root@centos-3 ~]# export name2=y //在子bash中继续定义变量 name2
[root@centos-3 ~]# echo $name2 //子bash中显示变量
y
[root@centos-3 ~]# exit //退回父bash
exit
[root@centos-3 ~]# echo $name2 //在父bash中显示变量
[root@centos-3 ~]# //父bash中不存在变量 name2
命令执行的优先级:
==> alias
==> Compound Commands
==> function
==> build_in
==> hash
==> $PATH
==> error: command not found
默认的变量存放位置:(此文件顺序为开机登陆系统时,系统加载的顺序)
[root@centos-3 ~]# su - ldsly //完全切换用户时,四个变量文件按顺序全部加载 Last login: Sat Jun 3 00:40:14 CST 2017 on tty1 /etc/profile /home/ldsly/.bash_profile /home/ldsly/.bashrc /etc/bashrc [ldsly@centos-3 ~]$ exit logout [root@centos-3 ~]# su ldsly //不完全切换时,只加载对应用户家目录下的.bashrc和系统下的 bashrc文件
/home/ldsly/.bashrc /etc/bashrc [ldsly@centos-3 root]$ exit exit [root@centos-3 ~]# bash //开启子bash时,加载当前登录用户家目录下的.bashrc和系统下的bashrc文件 /root/.bashrc /etc/bashrc [root@centos-3 ~]#
PATH的查看和添加:
[ldsly@centos-3 ~]$ echo $PATH //查看PATH /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ldsly/.local/bin:/home/ldsly/bin [ldsly@centos-3 ~]$ export PATH=/usr/local/nginx/sbin:$PATH //临时添加/usr/local/nginx/sbin到PATH中 [ldsly@centos-3 ~]$ echo $PATH /usr/local/nginx/sbin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ldsly/.local/bin:/home/ldsly/bin [ldsly@centos-3 ~]$
想永久生效就添加就在/etc/bashrc文件中添加export PATH=/usr/local/nginx/sbin:$PATH
元字符
~:家目录,可使用cd ~ 快速返回家目录
[ldsly@centos-3 ~]$ pwd /home/ldsly [ldsly@centos-3 ~]$
``:取命令执行结果
[ldsly@centos-3 ~]$ echo `ls`
Desktop Documents Downloads Music Pictures Public Templates Videos
$():同上但是弥补了``嵌套的缺陷
[ldsly@centos-3 ~]$ a=`echo `ls`` //使用``发现$a的值并不是我们要的ls的显示结果 [ldsly@centos-3 ~]$ echo $a ls [ldsly@centos-3 ~]$ a=$(echo `ls`) //使用$() [ldsly@centos-3 ~]$ echo $a Desktop Documents Downloads Music Pictures Public Templates Videos [ldsly@centos-3 ~]$ a=$(echo $(ls)) //全部使用$() [ldsly@centos-3 ~]$ echo $a Desktop Documents Downloads Music Pictures Public Templates Videos [ldsly@centos-3 ~]$
!:取反
[ldsly@centos-3 ~]$ ls Desktop Documents Downloads Music Pictures Public Templates Videos [ldsly@centos-3 ~]$ ls D[!e]* Documents: Downloads: [ldsly@centos-3 ~]$
!:取历史命令
57 ls 58 history [ldsly@centos-3 ~]$ !57 ls Desktop Documents Downloads Music Pictures Public Templates Videos [ldsly@centos-3 ~]$
!:取最近的一次命令匹配
[ldsly@centos-3 ~]$ !ls ls /tmp/ anaconda.log storage.log hsperfdata_ldsly systemd-private-51ecaa2f4af14852be58a7b018bae8fb-cups.service-DR3a4Y hsperfdata_root systemd-private-51ecaa2f4af14852be58a7b018bae8fb-vmtoolsd.service-qkylGu ifcfg.log systemd-private-b3d2522c2a1641249fba9275ac0c283a-cups.service-fCjOWS ks-script-JUZxd9 systemd-private-b3d2522c2a1641249fba9275ac0c283a-vmtoolsd.service-DRnv3k nginx-1.12.0.tar.gz tracker-extract-files.0 packaging.log tracker-extract-files.1000 program.log yum.log sensitive-info.log [ldsly@centos-3 ~]$
!:命令的返回值取反
[ldsly@centos-3 ~]$ ls Desktop Documents Downloads Music Pictures Public Templates Videos [ldsly@centos-3 ~]$ echo $? //取上一次命令的结果返回值 0 //0为真 非0为假,一般可理解为,上一条命令执行成功了返回0,执行失败返回非0值。范围:0-255 [ldsly@centos-3 ~]$ ! ls Desktop Documents Downloads Music Pictures Public Templates Videos [ldsly@centos-3 ~]$ echo $? 1 [ldsly@centos-3 ~]$
#:注释,井号后的内容不会被执行,仅用于注释
^:取反的意思,跟!差不多。但还是有区别
[root@centos-3 ~]# ls D[^e]* Documents: Downloads: [root@centos-3 ~]#
&:后台运行的意思
[root@centos-3 ~]# firefox & [1] 5484 [root@centos-3 ~]#
():在子进程中运行
[root@centos-3 ~]# a=1 [root@centos-3 ~]# echo $a 1 [root@centos-3 ~]# (a=10;echo $a) 10 [root@centos-3 ~]#
=:赋值
&&:逻辑与
||:逻辑或
|:管道符
>:重定向符
>>:追加重定向符
\:转义符
[root@centos-3 ~]# grep -n ‘root‘ /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [root@centos-3 ~]# grep -n ‘^root‘ /etc/passwd 1:root:x:0:0:root:/root:/bin/bash [root@centos-3 ~]#
$:行尾
[root@centos-3 ~]# grep -n ‘bash$‘ /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 43:ldsly:x:1000:1000:ldsly:/home/ldsly:/bin/bash [root@centos-3 ~]#
[]:中括号内任一字符
[root@centos-3 ~]# grep [bc] 1.txt abc abccc a/bc apobc [root@centos-3 ~]#
[^]:中括号内每个字符取反
[root@centos-3 ~]# grep [^bc] 1.txt a abc abccc a/bc apobc [root@centos-3 ~]#
[a-z]:小写字母
[A-Z]:大写字母
[a-Z]:小写和大写字母
[root@centos-3 ~]# grep [A-Z] 1.txt Abc aBC ABC [root@centos-3 ~]#
[0-9]:数字
[root@centos-3 ~]# grep [0-9] 1.txt ab2ccc a1/bc Abc4 aBC6 ABC7 [root@centos-3 ~]#
|:交替匹配|两边的任意一项
[root@centos-3 ~]# egrep ‘ABC|abc‘ 1.txt abc1 abc2 ABC1 ABC2 [root@centos-3 ~]#
标签:family 单词 sys documents system video mtools size service
原文地址:http://www.cnblogs.com/ldsly/p/6935016.html