“.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
相关推荐
Wadli2 小时前
集群C++聊天服务器
服务器·开发语言·c++
X7x52 小时前
网工核心:直连 / 静态 / 动态路由全解,附华为 / 华三 / 思科配置 + 高级应用
运维·网络·网络协议·信息与通信
King's King2 小时前
自动化仓库节能方案
运维·自动化
北京耐用通信2 小时前
告别通讯掉线!耐达讯自动化Modbus转Profinet网关:工业现场的“定海神针”
服务器·人工智能·网络协议·自动化·信息与通信
Echoo华地2 小时前
用git diff快速比较文件夹差异并生成报告
linux·git·unix·repository·diff·branch
思麟呀2 小时前
HTTP的Cookie和Session
linux·网络·c++·网络协议·http
小明同学012 小时前
linux进程(下)
linux·服务器·c++
瀚高PG实验室2 小时前
瀚高数据库安全版4.5.8系列使用pg_cron定时任务
服务器·数据库·瀚高数据库
格林威2 小时前
工业视觉检测:OpenCV FPS 正确计算的方式
运维·人工智能·数码相机·opencv·机器学习·计算机视觉·视觉检测