Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux Shell 深度解析:終端機、解釋器、父子 Shell、變數作用域與啟動配置全景圖

編輯:Linux 教學

Shell 是使用者與核心對話的介面,也是強大的程式語言環境。本文從終端機與 Shell 關係切入,層層剖析父子 Shell 進程模型、啟動配置檔讀取順序、三類變數作用域、export 機制與跨進程傳值實戰。

終端機 ≠ Shell

  • 終端機:輸入/輸出裝置(硬體終端、虛擬終端 tty、終端機模擬器 GNOME Terminal、iTerm2)
  • Shell:運行在終端機上的解釋器程式(/bin/bash、/bin/zsh、/bin/fish)
  • 啟動終端機 → 自動啟動預設 Shell(echo $SHELL)

父 Shell 與子 Shell

# 終端機啟動 → 父 Shell (PID 1234)
$ bash script.sh
  # 父 Shell fork → 子 Shell (PID 5678) 執行腳本
  # 子 Shell 結束 → 回到父 Shell
  • 子 Shell 透過 fork() 建立,複製父進程記憶體空間
  • 腳本首行 #!/bin/bash 指定解釋器
  • 互動式執行 bash 也會建立子 Shell

Shell 啟動配置檔讀取順序(Bash)

  1. /etc/profile → 系統級,登入 Shell 讀取
  2. /etc/profile.d/*.sh → 模組化系統設定
  3. ~/.bash_profile | ~/.bash_login | ~/.profile → 使用者級,依序尋找**第一個存在**者讀取
  4. ~/.bashrc → 互動式 Shell 每次啟動讀取
  5. /etc/bashrc → 系統級互動式設定(由 ~/.bashrc 內部 source)

最佳實踐:在 ~/.bash_profile 加入 [[ -f ~/.bashrc ]] && source ~/.bashrc,統一配置入口。

三類變數完整對比

類型範例定義/修改子 Shell 可見持久性
內部變數$# $0 $? $$Shell 內建,唯讀❌進程存續期間
環境變數PATH HOME SHELL PS1export VAR=val✅ 複本登出失效,除非寫入配置檔
本地變數STR="Hi"VAR=val❌當前 Shell 存續期間

export 讓本地變數升級為環境變數

STR="Hello World"
./child.sh   # 子 Shell 看不到

export STR
./child.sh   # 子 Shell 可見

# 永久生效
echo 'export STR="Hello World"' >> ~/.bashrc

父子 Shell 變數隔離實證

# 父 Shell
export STR="Parent"
./child.sh
# child.sh 內容:
echo "Child sees: $STR"
STR="Child modified"
echo "Child now: $STR"

# 結果:
# Child sees: Parent
# Child now: Child modified
# 父 Shell echo $STR → Parent(未被修改)

子進程修改的是**副本**,不影響父進程。

子→父傳值:暫存檔法

# child.sh
RESULT="computed_value"
echo "$RESULT" > /tmp/child_result_$$

# 父 Shell
./child.sh
RESULT=$(cat /tmp/child_result_$$)
rm /tmp/child_result_$$

常用 Shell 內建指令速查

指令功能
set顯示所有本地+環境變數
unset VAR刪除變數
export VAR設定/升級環境變數
env顯示環境變數 / 清空環境執行指令
declare -x VAR同 export
readonly VAR唯讀變數

Shell 變數作用域鐵律:環境變數單向流動(父→子副本),本地變數不出閘,子進程無法反向修改父進程。將此圖譜內化,腳本除錯不再迷路。

Copyright © Windows教程網 All Rights Reserved