interactive shell:指令搭配 prompt 從標準輸入讀取,shell 需要支援一些互動所需要的動作。
- 沒有非選項的引數,或有選項 -s。其標準輸入及錯誤輸出連接到 isatty() 決定的終端機。或者有選項 -i。不能有選項 -c 在引數指定指令。
- 會設定 PS1 作為輸入指令的 prompt,且 $- 包含 i,讓 shell 指令檔或起始執行的檔案測試是否為 interactive。
當 bash 以名稱 sh 執行,會採用傳統 sh 的起始行為,並進入 posix 模式。
| shell | login (不管是否為 interactive) | interactive (不含 login) | 非 interacitve | |||
|---|---|---|---|---|---|---|
| bash | sh | bash | sh | bash | sh | |
| 啟始 | /etc/profile ~/.bash_profile ~/.bash_login ~/.profile 三者之一 | /etc/profile ~/.profile | /etc/bash.bashrc ~/.bashrc | . "$ENV" | . "$BASH_ENV" | 無 |
| 選項 | --noprofile | --norc --rcfile file | 無 | 無 | ||
| 結束 | ~/.bash_logout | 無 | 無 | 無 | ||
如果 shell 開始時 effective user (group) ID 跟 real user (group) ID 不同,啟始檔案不執行,且不自環境繼承 shell function,忽略變數 SHELLOPTS、BASHOPTS、CDPATH、和 GLOBIGNORE。此時選項 -p 沒設,effective user ID 設為 real user ID。
判定是否在互動模式
方式一:看 $- 是否有 icase "$-" in
*i*) echo This shell is interactive ;;
*) echo This shell is not interactive ;;
esac
方式二:PS1 有設
if [ -z "$PS1" ]; then
echo This shell is not interactive
else
echo This shell is interactive
fi
參考來源
man bash INVOCATION 節延伸閱讀
- 在 interactive shell 選項 -s 可用來設定位置參數
- 互動式 shell 的行為:https://www.gnu.org/software/bash/manual/html_node/Interactive-Shell-Behavior.html#Interactive-Shell-Behavior
沒有留言:
張貼留言