迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

在终端中运行 Bash 脚本

作者:迹忆客 最近更新:2023/03/17 浏览次数:

本文演示了使用 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.

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便