Note1: Shell
Note1: Shell
B站上找了一个短的Shell教程;观看,敲代码,做笔记以资消化。
start date:13 July 2026, Monday
starting point:G2410 Hongqiao-Hankou
K1: Shell的定义
Shell是一个命令行解释器,它接收应用程序/用户命令,然后调用操作系统内核。
简单来说,它是外部应用程序和Linux内核沟通的桥梁。
Shell还是一个功能相当强大的编程语言,易于编写和调试,灵活性强。
K2: Shell解释器
/sh 的默认指向会随着Linux的发行版不同而改变
在CentOS(RedHat系)中,/bin/sh 默认指向/bin/bash。【Q1】
在Ubuntu(Debian系)中,/bin/sh 默认指向/bin/dash。【Q2】
这两个系统中,$SHELL 指向的都是 bash。
ls -l /bin/sh可查看/sh指向,以下是两个不同系统的输出结果。
Aliyun服务器:
lrwxrwxrwx. 1 root root 4 Oct 31 2024 /bin/sh -> bash
Ubuntu虚拟机:
lrwxrwxrwx 1 root root 4 9月 11 2022 /bin/sh -> dash*
Q1: bash 与 dash 的区别?
dash (Debian Almquist SHell): 符合 POSIX 标准【R1】,同时加入了少量 Berkeley 扩展;
bash (Bourne Again SHell): 符合 POSIX 标准【R1】,提供可选的严格合规模式,同时包含了大量超出该标准的扩展(俗称 GNU Bash 扩展)。
-
MANUAL DESCRIPTION
OverviewThe shell is a command that reads lines from either a file or the terminal, interprets them, and generally executes other commands. It is the program that is running when a user logs into the system (although a user can select a different shell with the chsh(1) command). The shell implements a language that has flow control constructs, a macro facility that provides a variety of features in addition to data storage, along with built in history and line editing capabilities. It incorporates many features to aid interactive use and has the advantage that the interpretative language is common to both interactive and non-interactive use (shell scripts). That is, commands can be typed directly to the running shell or can be put into a file and the file can be executed directly by the shell.
-
(man dash)
dash is the standard command interpreter for the system. The current version of dash is in the process of being changed to conform with the POSIX 1003.2 and 1003.2a specifications for the shell. This version has many features which make it appear similar in some respects to the Korn shell, but it is not a Korn shell clone (see ksh(1)). Only features designated by POSIX, plus a few Berkeley extensions, are being incorporated into this shell. -
(man bash)
Bash is a command language interpreter that executes commands read from the standard input, from a string, or from a file. It is a reimplementation and extension of the Bourne shell, the historical Unix command language interpreter. Bash also incorporates useful features from the Korn and C shells (ksh and csh).POSIX is the name for a family of computing standards based on Unix. Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard **1003.1**). Bash POSIX mode (hereafter referred to as posix mode) changes the shell's behavior where its default operation differs from the standard to strictly conform to the standard.
-
R1: POSIX 标准的定义
POSIX (Portable Operating System Interface) 是由 IEEE 发起,Austin Group 维护和发展的一系列标准,用于维持不同操作系统间的兼容性。适用于类Unix系统(Linux、macOS、各种BSD等),确保为该系统编写的程序、脚本和用户命令,能在任何遵循该标准的其他系统上无障碍地运行。
R2: POSIX 的版本历史
POSIX originally consisted of a single document for core services but over time additional documents were published to extend and revise the specification. Before 1997, POSIX comprised multiple documents that were published over the course of several years. After 1997, the Austin Group produces specifications titled Single UNIX Specification (SUS).
在早期的POSIX标准体系中,1003.1和1003.2是两份独立的文档,分别负责系统接口和Shell/工具这两大块。
后来,两个标准被合并成了一个统一的文档,即 POSIX.1。
Q2: 为何Ubuntu的/bin/sh和$SHELL 指向的解析器不同?
它们服务的对象和场景不同。
/bin/sh:系统默认的 POSIX Shell$SHELL:你的登录 Shell(Login Shell)
前者服务于系统脚本的兼容性和启动效率,后者反映的是用户登录后的交互环境。
前者使用 dash 可提高启动速度。dash 比 bash 体积更小、依赖更少,执行脚本时占用的内存和 CPU 开销也更低。
后者使用 bash 可提高易用性。bash有众多dash 没有的功能,诸如:历史命令(history)、自动补全(tab)、数组(arr=(a b c))、关联数组(declare -A)、命令替换($(...))、算术拓展(((i++)))。
K3: 脚本的基本格式
脚本以 #!/bin/bash 开头。(Shebang指定解析器)
注意事项:
- 明确使用
#!/bin/bash,强制指定Shell解析器类型。不要用#!/bin/sh,保证不同系统中,脚本行为的一致性。 - 如果需要给未知脚本赋予可执行权限(
chmod +x)并使用./运行,必须检查脚本第一行的解释器指向哪里。
K4: 脚本的执行方式
bash/sh+ 脚本的相对路径或绝对路径(无需可执行权限)- 赋予脚本可执行权限后,输入相对路径或绝对路径
演示:
# Step1: 赋予脚本echo.sh可执行权限
csc@csc-VirtualBox:~/learnShell$ chmod 777 echo.sh
# Step2.1: 相对路径
csc@csc-VirtualBox:~/learnShell$ ./echo.sh
Alice is the best!
# Step2.2: 绝对路径
csc@csc-VirtualBox:~/learnShell$ /home/csc/learnShell/echo.sh
Alice is the best!
E1: echo.sh
需求:创建一个Shell脚本,输出Alice
E2: batch.sh
需求:在/home/csc/learnShell下创建wife.txt,并在wife.txt文件中增加”Alice“。
资料
讲义:尚硅谷2018
评论