1.初始shell
shell是系统的用户界面 ,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送 入内核去执行。实际上shell是一个命令解释器,它解释用户输入的命令并且把用户的意图传达给内核。 (可以理解为用户与内核之间的翻译官角色)
我们可以使用shell实现对Linux系统单的大部分管理,例如:
- 文件管理
- 用户管理
- 权限管理
- 磁盘管理
- 软件管理
- 网络管理
- ….
使用shell的两种方式
- 输入命令 效率低 适合少量的工作
- shell script(脚本) 效率高 适合完成复杂,重复性工作
2.bash shell 提示符
[root@xwz ~]# echo $PS1 //PS1:系统终端命令提示符[\u@\h \W]\$[root@xwz ~]# date2025年 08月 21日 星期三 11:34:43 CST[root@xwz ~]# whoamiroot[root@xwz ~]# useradd xy[root@xwz ~]# passwd xy更改用户 xy 的密码 。新的 密码:重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。
3.shell语法
命令 选项 参数
- 命令:整条shell命令的主体
- 选项:会影响会微调命令的行为,通常以-或者—开头
- 参数:命令作用的对象(长参数、短参数)
[root@localhost ~]# ls[root@localhost ~]# ls -a[root@[root@localhost ~]# ls -a /home
4.bash基本特性
自动补全
按tab键即可出现提示或者路径补全
[root@localhost ~]# cat /etc/syscsysconfig/ sysctl.conf sysctl.d/[root@localhost ~]# cat /etc/sysconfig/netnetconsole network network-scripts/[root@localhost ~]# cat /etc/sysconfig/network
快捷键
ctrl+ 键
历史命令
[root@localhost ~]# history1 echo $Ps12 echo $PS13 vi /root/.bashrc4 whoami5 date6 useradd xy7 passwd xy8 useradd xy9 passwd xy
查看历史命令:history(缓存数据)
历史命令文件:~/.bash_history
- 历史命令文件里为上一次登陆shell时所保留的命令
- 登录shell时会读取历史命令文件,并将后续操作命令添加到历史配置文件中,下一次登陆才能看到
- 缓存中的内容并没有立刻写入~/.bash_history,只有当当前shell关闭时才会将内存内容写入,下一次登陆才能看到
选项:
- -a : 追加本次会话执行的命令历史列表至历史文件中
- -d 行号:删除历史中指定的命令
- -c: 清空历史命令
- 光标上下键
- ctrl+r: 搜索历史命令(输入一段某条命令的关键字:必须是连续的)
- !n:. 执行历史命令中第n条命令
- !字符串:搜索历史命令中最近一个以xxxx字符开头的命令,例如!ser
- !代表的是上一个命令本 身
[root@salted ~]# history1*2 pwd3 ll4 cd[root@salted ~]# !3ll总用量 4-rw-------. 1 root root 1241 1月 26 10:12 anaconda-ks.cfg[root@salted ~]# !hishistory1 history2 pwd3 ll[root@salted ~]# cd /etc/sysconfig/[root@salted sysconfig]# !$/etc/sysconfig/bash: /etc/sysconfig/: 是一个目录[root@salted sysconfig]# cd !$cd /etc/sysconfig/
命令别名
# 建立别名(临时的,仅在当前shell生效)[root@salted ~]# alias resetname='hostnamectl set-hostname'[root@salted ~]# resetname xy[root@salted ~]# bash[root@xy ~]##取消别名[root@xy ~]# unalias resetnamebash: unalias: resetname: 未找到[root@xy ~]# resername saltedbash: resername: 未找到命令#查看系统当前别名[root@salted ~]# alias#永久别名[root@salted ~]# vi /etc/bashrcalias resetname='hostnamectl set-hostname'
[root@xwz ~]# /bin/lsanaconda-ks.cfg home[root@xwz ~]# ls # 别名优先anaconda-ks.cfg home[root@xwz ~]# \ls # 跳过别名anaconda-ks.cfg home
5.获得帮助
命令:—help
[root@salted ~]# ls --help用法:ls [选项]... [文件]...
ls常见选项:
man手册名(针对命令帮助,针对配置文件帮助,针对函数 帮助)
yum -y install man-pages-zh-CN.noarchecho "alias cman='man -M /usr/share/man/zh_CN'" >> .bashrcsource .bashrc
- 命令帮助:章节1,章节8
- 函数帮助:章节2,章节3
- 文件格式:章节5
一般情况下不需要使用章节号
[root@xwz ~]# man ls[root@xwz ~]# man man[root@xwz ~]# man useradd[root@xwz ~]# man setfacl /EXAMPLES
按章节查询
[root@xwz ~]# /usr/bin/passwd # 修改用户口令命令[root@xwz ~]# /etc/passwd # 包含用户信息的配置文件[root@xwz ~]# man -f passwdsslpasswd (1ssl) - compute password hashespasswd (1) - update user's authentication tokens[root@xwz ~]# man 1 passwd[root@xwz ~]# man 5 passwd
在所有章节中查询
[root@xwz ~]# man -a passwd
