在終端中執行 Bash 指令碼
Fumbani Banda
2023年1月30日
2022年5月14日
![在終端中執行 Bash 指令碼](/img/Linux/ag%20feature%20image%20-%20run%20bash%20script%20in%20terminal.png)
本教程演示了使用 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.
Author: Fumbani Banda