"...":變數展開、命令替換、轉義字元生效'...':強引號,內容原樣輸出`...` 或 $(...):命令替換,執行子命令並取標準輸出export VAR → 升為環境變數 (全域,子進程繼承)export,或用 source/. 載入腳本
| 類型 | 登入 Shell | 非登入互動 Shell |
|---|---|---|
| 系統級 | /etc/profile | /etc/bashrc |
| 用戶級 | ~/.bash_profile | ~/.bashrc |
系統級對所有用戶生效,用戶級僅對當前用戶生效。
bash script.sh (不需執行權限)chmod +x script.sh; ./script.sh (需 Shebang #!/bin/bash)執行時會 fork 子 Shell,子 Shell 環境變更不影響父 Shell。
| 語法 | 行為 |
|---|---|
| ${var:-default} | var 未設或為空 → default |
| ${var:=default} | 同上並賦值給 var |
| ${var:+other} | var 已設且非空 → other |
| ${!prefix*} | 列出所有以 prefix 開頭的變數名 |