“.sh”文件

在Linux中,.sh文件是Shell脚本文件。本质上是一个包含一系列Linux命令的纯文本文件,用于自动化执行任务

基本概念:

  • 扩展名含义:.sh表示"Shell"(命令行解释器)
  • 文件性质:纯文本文件,可以用任何文本编辑器(vi、nano、gedit等)打开
  • 作用:将多个命令按顺序写入一个文件,批量执行,避免重复手动输入
  • Shebang 必须写在第一行 :如#!/bin/bash 告诉系统用哪个shell解释

注意:

  • .bashrc配置文件,每次打开终端自动执行

  • .sh 文件是脚本文件,手动按需执行

不过它们本质上都是shell命令的集合,只是用法和触发方式不同

新建一个脚本文件:

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ touch test1.sh
bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ls
test1.sh
bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ vim ./test1.sh
#!/bin/bash
## 注意:上面这一行不是注释!它是脚本中的一个特殊标记,叫做Shebang(或称解释器指令)
# 其中,"#!"是一个特殊标记,"/bin/bash"是Bash shell的路径。系统看到这个特殊标记后就要用/bin/bash这个程序来执行这个脚本

echo "Hello World!"
echo "2026/4/17 16:08"

按ESC键回到Vim的普通模式,输入":wq"保存并退出

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ls -l
total 4
-rw-rw-r-- 1 xiaoyu xiaoyu 346 Apr 17 16:08 test1.sh
bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ./test1.sh
-bash: ./test1.sh: Permission denied
bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ bash ./test1.sh
Hello World!
2026/4/17 16:08
对比项 ./test1.sh bash test1.sh
是否需要执行权限 ✅ 必须 (chmod +x) ❌ 不需要
依赖 Shebang ✅ 需要 (#!/bin/bash) ❌ 忽略 Shebang
指定解释器 由 Shebang 决定 强制使用 bash
工作原理 作为独立程序执行 将脚本作为参数传给 bash
可移植性 低(依赖 Shebang 路径) 高(明确使用 bash)

执行.sh脚本文件的方法1:作为可执行程序运行(最常用)

需要两步:

bash 复制代码
# 1. 给文件添加执行权限
chmod +x myscript.sh

# 2. 执行(需要指定路径)
./test1.sh          # 当前目录
/home/xiaoyu/LearnTests/test1.sh   # 绝对路径

方法2:用bash命令执行(不需要执行权限)

bash 复制代码
bash test1.sh
# 或
sh test1.sh

方法3:使用source命令执行(在当前shell中)

bash 复制代码
source myscript.sh
# 或
. myscript.sh

三种执行方式的区别:

方式 执行权限要求 运行环境 环境变量是否保留(如果脚本中有) 典型用途
./script.sh 需要 chmod +x 子shell ❌ 不保留 普通脚本
bash script.sh 不需要 子shell ❌ 不保留 快速测试
source script.sh 不需要 当前shell ✅ 会保留 加载配置

第二个示例:

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ touch test2.sh
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ls
test1.sh  test2.sh
bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ vim test2.sh
# 没有指明Shebang,即Shebeng被忽略
echo "2026/4/17 16:35"
bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ test2.sh
test2.sh: command not found
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ./test2.sh
-bash: ./test2.sh: Permission denied

第一次失败:command not found

表明系统在PATH环境变量中找不到test2.sh这个命令

第二次失败:Permission denied

表明文件存在且路径正确,但是没有执行权限

失败原因分析:

第一次失败中,你直接输入test2.sh,没有指定路径。那么系统会在PATH环境变量定义的目录中查找这个命令,但是当前目录(~/LearnTests)不在PATH环境变量中,因此系统找不到test2.sh这个命令

推荐的解决方法:指定路径(就像第二次失败的指令一样,虽然说第二次也失败了)

第二次失败中,test2.sh文件没有执行权限(x权限)

验证test2.sh的权限:

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ls -l ./test2.sh
-rw-rw-r-- 1 xiaoyu xiaoyu 67 Apr 17 16:36 ./test2.sh

解决方法:添加权限

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ chmod +x test2.sh
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ls -l test2.sh
-rwxrwxr-x 1 xiaoyu xiaoyu 67 Apr 17 16:36 test2.sh
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ./ test2.sh
-bash: ./: Is a directory
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ./test2.sh
2026/4/17 16:35

第三个示例:

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ vim test3.sh
# 没有指明Shebang,即Shebeng被忽略
echo "2026/4/17 16:35"
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ test3.sh
test3.sh: command not found
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ test2.sh
test2.sh: command not found
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ./test3.sh
-bash: ./test3.sh: Permission denied
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ bash test3.sh
2026/4/17 16:52

使用bash命令可以不用权限就执行.sh脚本文件

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ vim myscript.py
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ./myscript.py
-bash: ./myscript.py: Permission denied
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ bash myscript.py
myscript.py: line 2: syntax error near unexpected token `"This is Python3"'
myscript.py: line 2: `print("This is Python3")'
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ls -l myscript.py
-rw-rw-r-- 1 xiaoyu xiaoyu 44 Apr 17 16:58 myscript.py
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ chmod -x myscript.py
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ls -l myscript.py
-rw-rw-r-- 1 xiaoyu xiaoyu 44 Apr 17 16:58 myscript.py

先分析一下上述输入输出:

使用vim编辑器创建了一个新的python文件:myscript.py

我在myscript.py中输入了:

bash 复制代码
#!/usr/bin/python3
print("This is Python3")

然后输入:./myscript.py想把这个文件当成可执行文件执行,但是输出表示没有权限

然后我使用bash命令,即用bash解释器去执行Python代码,但是bash不认识Python的print()语法,所以报语法错误

然后我使用ls命令查看了myscript.py的权限,知道了这个文件没有x权限,所以不能被当作可执行文件一样被执行

于是我想给它添加权限,但是我命令输错了!-x的意思是:移除执行权限

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ chmod +x myscript.py
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ls -l myscript.py
-rwxrwxr-x 1 xiaoyu xiaoyu 44 Apr 17 16:58 myscript.py
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ ./myscript.py
This is Python3

或用Python解释器直接运行:

bash 复制代码
xiaoyu@xiaoyu-VMware-Virtual-Platform:~/LearnTests$ python3 myscript.py
This is Python3
相关推荐
A小辣椒17 小时前
TShark:Wireshark CLI 功能
linux
A小辣椒21 小时前
TShark:基础知识
linux
AlfredZhao1 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush43 天前
嵌入式linux学习记录十四、术语
linux·嵌入式