Linux shell的特性

本文围绕Linux系统展开,介绍了命令历史的查看、使用及保存,管道和IO重定向的功能,命令别名的定义与删除,命令行编辑的快捷键,命令行展开的补齐和替换方法,还提及命令行通配、变量和编程等内容,为Linux使用者提供操作技巧。

1、命令历史——history

Linux会自动记录系统过去执行的命令,并保存在内存的缓冲区中,在每个用户的家目录下,命令历史被保存在隐藏文件.bash_history中。

查看历史命令记录的数目

echo $HISTSIZE,默认记录1000条命令

ubuntu@ubuntu:~/test_file$ echo $HISTSIZE
1000

history命令的一些常见用法

-c:清空当前shell下所有命令历史,输入的命令history -c 不被保留

-d:删除某一条命令

ubuntu@ubuntu:~/test_file$ history 
    1  history 
    2  ll
    3  ls
    4  history 
ubuntu@ubuntu:~/test_file$ history -d 2
ubuntu@ubuntu:~/test_file$ history 
    1  history 
    2  ls
    3  history 
    4  history -d 2
    5  history 

-w:将命令历史保存到文件中

buntu@ubuntu:~/test_file$ history -w ./history.out
ubuntu@ubuntu:~/test_file$ cat history.out 
history 
ls
history 
history -d 2
history 
ls
rm history.out 
ls
history -w ./history.out

历史命令使用技巧

!n:执行命令历史中的第n条命令(n是命令历史编号)

ubuntu@ubuntu:~/test_file$ !2
ls ./
123  a  a123  aa123  ab  b  b123  history.out

!-n:执行命令历史中的倒数第n条命令(n是命令历史编号)

ubuntu@ubuntu:~/test_file$ !-2
ls ./
123  a  a123  aa123  ab  b  b123  history.out

!word:执行命令历史中最近一次以word开始的命令(word必须能够唯一的标识用户想执行的命令)

ubuntu@ubuntu:~/test_file$ !cat
cat a
hello
world!

!!:执行上一条命令

ubuntu@ubuntu:~/test_file$ !!
cat a
hello
world!

!$:引用一个命令的最后一次参数

ubuntu@ubuntu:~/test_file$ rm ./!$
rm ./a

2、管道,IO重定向

管道

连接多条命令,把前一条命令的输出结果作为后一条命令的输入条件(组合小程序,实现大功能)

command1 | command2 | commad3 ....

ubuntu@ubuntu:~/test_file$ pwd
/home/ubuntu/test_file

# pwd的返回结果是当前目录,通过管道符将返回结果传递给ls作为参数
ubuntu@ubuntu:~/test_file$ pwd | ls   
123  a  a123  aa123  ab  b  b123  history.ouot

IO重定向

Linux系统中程序的输入和输出都配置有默认的输入和输出数据源,重定向是指对于程序的输入和输出,不按照默认数据源执行,而是重新指向其他文件或设备

输出重定向

符号意义
>覆盖输出重定向
>>追加输出重定向
2>错误覆盖输出重定向
2>>错误追加输出重定向
&>混合覆盖输出重定向
&>>回合追加输出重定向
ubuntu@ubuntu:~/test_file$ cat a > redirect.out
ubuntu@ubuntu:~/test_file$ cat redirect.out 
hello
world!

ubuntu@ubuntu:~/test_file$ cat a >> redirect.out 
ubuntu@ubuntu:~/test_file$ cat redirect.out 
hello
world!
hello
world!

ubuntu@ubuntu:~/test_file$ cat c 2> redirectErr.out 
ubuntu@ubuntu:~/test_file$ cat redirectErr.out 
cat: c: 没有那个文件或目录

ubuntu@ubuntu:~/test_file$ cat c 2>> redirectErr.out 
ubuntu@ubuntu:~/test_file$ cat redirectErr.out 
cat: c: 没有那个文件或目录
cat: c: 没有那个文件或目录

ubuntu@ubuntu:~/test_file$ cat a &>> redirect.out 
ubuntu@ubuntu:~/test_file$ cat c &>> redirect.out 
ubuntu@ubuntu:~/test_file$ cat redirect.out 
hello
world!
hello
world!
hello
world!
cat: c: 没有那个文件或目录

输入重定向

符号意义
<输入重定向
ubuntu@ubuntu:~/test_file$ cat < a
hello
world!

3、命令别名

alias定义的别名只在当前shell生效

添加别名:alias COMM_ALIAS=COMMAND,COMMAND最好使用' '引起来,单独使用alias列出当前shell中所有命令别名

删除别名:unalias COMM_ALIAS

# 添加别名
ubuntu@ubuntu:~/test_file$ alias ca='cat /home/ubuntu/test_file/a'
ubuntu@ubuntu:~/test_file$ ca
hello
world!

# 查看当前shell所有别名
ubuntu@ubuntu:~/test_file$ alias
alias ca='cat /home/ubuntu/test_file/a'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

# 删除别名
ubuntu@ubuntu:~/test_file$ unalias ca
ubuntu@ubuntu:~/test_file$ alias
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

4、命令行编辑

在shell中敲完一条命令,使用键盘的左/右方向键,能够是光标左右移动,移动太慢!系统中提供了一些快捷键对光标进行操作。

快捷键意义
ctrl + a光标快速跳转到命令行的行首
ctrl + e光标快速跳转到命令行的行尾
ctrl + u快速删除光标位置到命令行行首的字符
ctrl + k快速删除光标位置到命令行行尾的字符
ctrl + l清屏(命令clear效果相同)

5、命令行展开

命令/路径和文件补齐

命令补齐,是在PATH变量搜索命令并补齐。注意!PATH变量必须正常,输入要补齐命令的字符数一定能够唯一标识这条命令。

路径和文件补齐,是在系统路径中查找,输入的内容必须唯一标识目标。

按一次tab键补齐命令/路径和文件名

连按tab键两次,列出以当前输入字符开始的所有的命令/路径和文件名

注意!命令行补齐功能不能补齐选项。

命令替换

命令替换(经常用到shell脚本编程中),$(command)或者 `command`(两边是反撇号)
命令替换就是把命令中的子命令替换成子命令执行结果的过程

​ " "(双引号):弱引用(可是实现变量的替换,把变量名替换为变量值)
​ ` `(反撇号):命令引用
​ ’ '(单引号):强引用(不能完成变量替换)

6、命令行通配:globbing

符号含义
*匹配任意长度的任意字符
?匹配任意单个字符
[ ]匹配制定范围内的任意单个字符[ab] [a-z] [A-Z] [a-zA-Z] [0-9] [a-zA-Z0-9]
[^ ]匹配指定范围外的任意单个字符[^a-z][^0-9]
[:space:]空格
[:punct:]所有标点符号
[:lower:]所有小写字母
[:upper:]所有大写字母
[:alpha:]所有字母,包含大小写字母
[:digit:]所有数字
[:alnum:]所有数字和大小写字母
ubuntu@ubuntu:~/test_file$ ls
123  a  a123  aa123  ab  b  b123

ubuntu@ubuntu:~/test_file$ ls *
123  a  a123  aa123  ab  b  b123

ubuntu@ubuntu:~/test_file$ ls a*
a  a123  aa123  ab

ubuntu@ubuntu:~/test_file$ ls a?
ab

ubuntu@ubuntu:~/test_file$ ls a?*
a123  aa123  ab

ubuntu@ubuntu:~/test_file$ ls [a,b]*
a  a123  aa123  ab  b  b123

ubuntu@ubuntu:~/test_file$ ls [^a]*
123  b  b123

ubuntu@ubuntu:~/test_file$ ls ?[^a]*
123  a123  ab  b123

ubuntu@ubuntu:~/test_file$ ls [[:lower:]]
a  b

7、变量

8、编程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值