cd /path/to/scriptchmod +x script.sh./script.sh示例:在 /tmp 目錄下運行 1.sh
$ cd /tmp
$ chmod +x 1.sh
$ ./1.sh
若腳本需要 Root 權限,可在命令前加 sudo:sudo ./1.sh。
無需執行權限,直接指定解析器:
sh script.sh
# 或
bash script.sh
此方式下腳本首行 #!/bin/bash 等 Shebang 會被忽略,由指定的 Shell 解析。
chmod +x 或使用 sh script.sh。\r,用 dos2unix script.sh 或 sed -i 's/\r$//' script.sh 轉換。man sh 或 man bash 了解更多選項。bash -x script.sh 逐行顯示執行過程。習慣使用 chmod +x + ./script.sh 可保留 Shebang 指定的解析器,更符合腳本可移植性原則;sh script.sh 適合快速測試或無寫入權限場景。