Shebang

開頭的#!稱為 shebang,也叫作 hashbang,由井字號加上驚嘆號組成。

#!/usr/bin/env bash

檢查是否以 root 執行

if [ $(id -u) != "0" ]; then
    echo "The script must be run as root."
    exit 1
fi

單、雙引號差異

NAME="Chiahong"
echo "Hello $NAME" # Hello Chiahong
echo 'Hello $NAME' # Hello $NAME

迴圈

for ((i = 0; i < 10; i++)); do
    echo $i
done
for i in {1..10}; do
    echo $i
done
for i in {1..10..2}; do
    echo $i # 1 3 5 7 9
done
while true; do
    # 無窮迴圈
done
# /etc 中的所有檔案
for i in /etc/*; do
    echo $i
done