Linux笔记 -- Shell

快捷键
快捷键功效
Ctrl+a跳至最前
Ctrl+e跳至最后
Ctrl+u向左删除
Ctrl+k向后删除
Ctrl+l清屏
Ctrl+c终端当前程序
Ctrl+z挂起当前程序
Ctrl+s锁定当前屏幕
Ctrl+q解锁当前屏幕
Ctrl+d退出当前窗口
Ctrl+r查找最近使用过的命令
![history-num]执行历史命令
特殊符号
符号说明
‘’原模原样输出
“”解析$(变量)、``(命令)、\(转义符)
``执行命令
$()执行命令
$(())执行运算
$[]执行运算
[root@ansible ~]# echo '$PATH'
$PATH
[root@ansible ~]# 
[root@ansible ~]# echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@ansible ~]# 
[root@ansible ~]# echo `date`
Mon Oct 19 16:27:48 CST 2020
[root@ansible ~]# 
[root@ansible ~]# echo $(date)
Mon Oct 19 16:27:55 CST 2020
[root@ansible ~]# 
[root@ansible ~]# a=3
[root@ansible ~]# b=4
[root@ansible ~]# echo "$a+$b"
3+4
[root@ansible ~]# echo "$(($a+$b))"
7
[root@ansible ~]# echo "$[$a+$b]"
7
[root@ansible ~]# 
重定向
[root@ansible ~]# # 标准输出与错误输出重定向至同一位置
[root@ansible ~]# ll /usr /not-exist &> log
[root@ansible ~]# head -5 log
ls: cannot access '/not-exist': No such file or directory
/usr:
total 116
dr-xr-xr-x.  2 root root 24576 Oct 19 09:38 bin
drwxr-xr-x.  2 root root     6 May 11  2019 games
[root@ansible ~]# 
[root@ansible ~]# # 标准输出与错误输出重定向至不同位置
[root@ansible ~]# ll /usr /not-exist > stdout 2> stderr
[root@ansible ~]# head -3 stdout 
/usr:
total 116
dr-xr-xr-x.  2 root root 24576 Oct 19 09:38 bin
[root@ansible ~]# cat stderr
ls: cannot access '/not-exist': No such file or directory
[root@ansible ~]# 
多命令执行
符号说明
;多命令一块执行
&&前面的命令正确执行,后面的命令才会执行
||前面的命令不正确执行,后面的命令才会执行
[root@ansible ~]# date; ls /usr
Mon Oct 19 16:21:45 CST 2020
bin  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[root@ansible ~]# dates; ls /usr
-bash: dates: command not found
bin  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[root@ansible ~]# 
[root@ansible ~]# date && ls /usr
Mon Oct 19 16:23:50 CST 2020
bin  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[root@ansible ~]# dates && ls /usr
-bash: dates: command not found
[root@ansible ~]# 
[root@ansible ~]# date || ls /usr
Mon Oct 19 16:22:44 CST 2020
[root@ansible ~]# dates || ls /usr
-bash: dates: command not found
bin  games  include  lib  lib64  libexec  local  sbin  share  src  tmp
[root@ansible ~]# 
字符串截取
格式说明
${string: start :length}start 至 start+length
${string: start}start 至 end
${string: 0-start :length}end-start 至 end-start+length
${string: 0-start}end-start 至 end
${string#*chars}从 string 字符串第一次出现 chars 的位置开始,截取 chars 右边的所有字符。
${string##*chars}从 string 字符串最后一次出现 chars 的位置开始,截取 chars 右边的所有字符。
${string%chars*}从 string 字符串第一次出现 chars 的位置开始,截取 chars 左边的所有字符。
${string%%chars*}从 string 字符串最后一次出现 chars 的位置开始,截取 chars 左边的所有字符。
[root@ansible ~]# string=123456789
[root@ansible ~]# echo ${string:0:5}
12345
[root@ansible ~]# echo ${string:5}
6789
[root@ansible ~]#
[root@ansible ~]# echo $string
123456789
[root@ansible ~]# echo ${string:0-5:3}
567
[root@ansible ~]# echo ${string:0-5}
56789
[root@ansible ~]# 
[root@ansible ~]# url=www.baidu.com
[root@ansible ~]# echo ${#url}
13
[root@ansible ~]# 
[root@ansible ~]# echo ${url#*.}
baidu.com
[root@ansible ~]# echo ${url##*.}
com
[root@ansible ~]# 
[root@ansible ~]# echo $url
www.baidu.com
[root@ansible ~]# echo ${url%.*}
www.baidu
[root@ansible ~]# echo ${url%%.*}
www
[root@ansible ~]# 
元字符
字符说明
IFS分隔符
CR换行符
=设定变量
$取变量值或取运算值
>重定向 stdout
<重定向 stdin
|管道符号
&将命令置于后台执行
( )将其内的命令置于 nested subshell 执行,或用于运算或命令替换
{ }将其内的命令置于 non-named function 中执行,或用在变量替换的界定范围
!运算意义上的非(not)的意思
#注释,常用在脚本中
\转移字符,去除其后紧跟的元字符或通配符的特殊意义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值