1.初始shell

shell是系统的用户界面 ,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送 入内核去执行。实际上shell是一个命令解释器,它解释用户输入的命令并且把用户的意图传达给内核。 (可以理解为用户与内核之间的翻译官角色)

我们可以使用shell实现对Linux系统单的大部分管理,例如:

  1. 文件管理
  2. 用户管理
  3. 权限管理
  4. 磁盘管理
  5. 软件管理
  6. 网络管理
  7. ….

使用shell的两种方式

  1. 输入命令 效率低 适合少量的工作
  2. shell script(脚本) 效率高 适合完成复杂,重复性工作

2.bash shell 提示符

  1. [root@xwz ~]# echo $PS1 //PS1:系统终端命令提示符
  2. [\u@\h \W]\$
  3. [root@xwz ~]# date
  4. 2025 08 21 星期三 11:34:43 CST
  5. [root@xwz ~]# whoami
  6. root
  7. [root@xwz ~]# useradd xy
  8. [root@xwz ~]# passwd xy
  9. 更改用户 xy 的密码
  10. 新的 密码:
  11. 重新输入新的 密码:
  12. passwd:所有的身份验证令牌已经成功更新。

3.shell语法

命令 选项 参数

  1. 命令:整条shell命令的主体
  2. 选项:会影响会微调命令的行为,通常以-或者—开头
  3. 参数:命令作用的对象(长参数、短参数)
  1. [root@localhost ~]# ls
  2. [root@localhost ~]# ls -a
  3. [root@[root@localhost ~]# ls -a /home

4.bash基本特性

自动补全

按tab键即可出现提示或者路径补全

  1. [root@localhost ~]# cat /etc/sysc
  2. sysconfig/ sysctl.conf sysctl.d/
  3. [root@localhost ~]# cat /etc/sysconfig/net
  4. netconsole network network-scripts/
  5. [root@localhost ~]# cat /etc/sysconfig/network

快捷键

ctrl+ 键

历史命令

  1. [root@localhost ~]# history
  2. 1 echo $Ps1
  3. 2 echo $PS1
  4. 3 vi /root/.bashrc
  5. 4 whoami
  6. 5 date
  7. 6 useradd xy
  8. 7 passwd xy
  9. 8 useradd xy
  10. 9 passwd xy

查看历史命令:history(缓存数据)

历史命令文件:~/.bash_history

  • 历史命令文件里为上一次登陆shell时所保留的命令
  • 登录shell时会读取历史命令文件,并将后续操作命令添加到历史配置文件中,下一次登陆才能看到
  • 缓存中的内容并没有立刻写入~/.bash_history,只有当当前shell关闭时才会将内存内容写入,下一次登陆才能看到

选项:

  • -a : 追加本次会话执行的命令历史列表至历史文件中
  • -d 行号:删除历史中指定的命令
  • -c: 清空历史命令
  1. 光标上下键
  2. ctrl+r: 搜索历史命令(输入一段某条命令的关键字:必须是连续的)
  1. !n:. 执行历史命令中第n条命令
  2. !字符串:搜索历史命令中最近一个以xxxx字符开头的命令,例如!ser
  3. !![](https://g.yuque.com/gr/latex?%EF%BC%9A.%20%E5%BC%95%E7%94%A8%E4%B8%8A%E4%B8%80%E4%B8%AA%E5%91%BD%E4%BB%A4%E7%9A%84%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E5%8F%82%E6%95%B0%20%EF%BC%8C%E5%BD%93%E4%B8%8A%E4%B8%80%E4%B8%AA%E5%91%BD%E4%BB%A4%E6%B2%A1%E6%9C%89%E5%8F%82%E6%95%B0%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C!#card=math&code=%2A%2A%EF%BC%9A.%20%E5%BC%95%E7%94%A8%E4%B8%8A%E4%B8%80%E4%B8%AA%E5%91%BD%E4%BB%A4%E7%9A%84%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E5%8F%82%E6%95%B0%20%EF%BC%8C%E5%BD%93%E4%B8%8A%E4%B8%80%E4%B8%AA%E5%91%BD%E4%BB%A4%E6%B2%A1%E6%9C%89%E5%8F%82%E6%95%B0%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%21)代表的是上一个命令本 身
  1. [root@salted ~]# history
  2. 1*
  3. 2 pwd
  4. 3 ll
  5. 4 cd
  6. [root@salted ~]# !3
  7. ll
  8. 总用量 4
  9. -rw-------. 1 root root 1241 1 26 10:12 anaconda-ks.cfg
  10. [root@salted ~]# !his
  11. history
  12. 1 history
  13. 2 pwd
  14. 3 ll
  15. [root@salted ~]# cd /etc/sysconfig/
  16. [root@salted sysconfig]# !$
  17. /etc/sysconfig/
  18. bash: /etc/sysconfig/: 是一个目录
  19. [root@salted sysconfig]# cd !$
  20. cd /etc/sysconfig/

命令别名

  1. # 建立别名(临时的,仅在当前shell生效)
  2. [root@salted ~]# alias resetname='hostnamectl set-hostname'
  3. [root@salted ~]# resetname xy
  4. [root@salted ~]# bash
  5. [root@xy ~]#
  6. #取消别名
  7. [root@xy ~]# unalias resetname
  8. bash: unalias: resetname: 未找到
  9. [root@xy ~]# resername salted
  10. bash: resername: 未找到命令
  11. #查看系统当前别名
  12. [root@salted ~]# alias
  13. #永久别名
  14. [root@salted ~]# vi /etc/bashrc
  15. alias resetname='hostnamectl set-hostname'
  1. [root@xwz ~]# /bin/ls
  2. anaconda-ks.cfg home
  3. [root@xwz ~]# ls # 别名优先
  4. anaconda-ks.cfg home
  5. [root@xwz ~]# \ls # 跳过别名
  6. anaconda-ks.cfg home

5.获得帮助

命令:—help

  1. [root@salted ~]# ls --help
  2. 用法:ls [选项]... [文件]...

ls常见选项:

man手册名(针对命令帮助,针对配置文件帮助,针对函数 帮助)

  1. yum -y install man-pages-zh-CN.noarch
  2. echo "alias cman='man -M /usr/share/man/zh_CN'" >> .bashrc
  3. source .bashrc
  • 命令帮助:章节1,章节8
  • 函数帮助:章节2,章节3
  • 文件格式:章节5

一般情况下不需要使用章节号

  1. [root@xwz ~]# man ls
  2. [root@xwz ~]# man man
  3. [root@xwz ~]# man useradd
  4. [root@xwz ~]# man setfacl /EXAMPLES

按章节查询

  1. [root@xwz ~]# /usr/bin/passwd # 修改用户口令命令
  2. [root@xwz ~]# /etc/passwd # 包含用户信息的配置文件
  3. [root@xwz ~]# man -f passwd
  4. sslpasswd (1ssl) - compute password hashes
  5. passwd (1) - update user's authentication tokens
  6. [root@xwz ~]# man 1 passwd
  7. [root@xwz ~]# man 5 passwd

在所有章节中查询

  1. [root@xwz ~]# man -a passwd