在终端中运行 Bash 脚本
Fumbani Banda
2023年1月30日

本教程演示了使用 bash
命令在终端中运行 bash 脚本并使其可执行的方法。
使用 bash
命令运行 Bash 脚本
要在 Linux 中运行 bash 脚本,你需要使用 bash
命令并指定要运行的脚本的名称。
printf "This is my first script.\n"
printf "This is fun.\n"
使用 bash
运行脚本:
bash script.sh
输出:
This is my first script.
This is fun.
使用 chmod
使 Bash 脚本可执行
chmod
用于修改文件的权限。在我们的例子中,chmod
添加了对 script.sh
的执行权限。我们现在可以通过 ./script.sh
运行脚本。
printf "This is my second script.\n"
printf "This is more fun.\n"
使脚本可执行:
chmod +x script.sh
运行脚本:
./script.sh
输出:
This is my second script.
This is more fun.
作者: Fumbani Banda