跳到主要內容

腳本裡 cd 成功,終端機卻沒移動?看懂 Bash 的工作目錄 / Why cd in a Script Does Not Move Your Terminal

目錄怎麼又回去了? / Why did the directory revert?

假設你在 Linux 的 Bash 終端機裡執行一支腳本,裡面明明寫了切換目錄,腳本也沒有報錯,結束後卻還停在原處。這通常不是路徑失靈,而是你把「另一個 shell 的工作目錄」,當成了眼前這個 shell 的狀態。

Imagine running a script in a Bash terminal on Linux. The script changes directory without an error, yet your terminal remains where it started. Usually the path is not broken: you have mistaken another shell’s working directory for the state of your current shell.

工作目錄是程序用來解讀相對路徑的起點,不是整台電腦共用的一個游標。以 bash go.sh 執行腳本時,新的 Bash 會繼承起始目錄;它之後呼叫 cd,只改變自己的工作目錄,並不把變更回寫給啟動它的 shell。[1]

The working directory is a process’s starting point for resolving relative paths, not a computer-wide cursor. Running bash go.sh starts another Bash that inherits the initial directory. Its subsequent cd changes its own working directory, without writing that change back to the shell that launched it. [1]

腳本 shell 繼承起始目錄,cd 只改變自身,外層目錄保持不變 / The script shell inherits its starting directory; cd changes only its own directory, leaving the outer shell unchanged.
圖一|繼承不等於回寫:腳本結束,外層仍在原處。原創示意圖,非產品或實測截圖。 / Figure 1 | Inheritance is not write-back: the outer shell stays put after the script exits. Original diagram, not a product or test screenshot.

同一行指令,不一定在同一個 shell / Same command, different shell

這裡談的是 Linux 上一般 Bash 指令的執行方式,不是圖形檔案管理員的導覽,也不是所有語言的特殊共享狀態。腳本仍然可以修改有權限的檔案;工作目錄不回寫,不代表它對外界沒有任何影響。

This discussion covers ordinary Bash command execution on Linux, not navigation in a graphical file manager or every language’s special shared state. A script can still modify files it has permission to write: not writing its working directory back does not mean it has no external effects.

所以,直接在提示字元輸入 cd 能留下效果,是因為它是 Bash 的內建指令,作用在目前的 shell。反過來,把 cd 放進括號形成子 shell,即使仍是內建指令,變更也留在子環境裡。判斷重點不只是哪個指令,而是它在哪個執行環境運作。[1][2]

Typing cd directly at the prompt persists because it is a Bash builtin acting in the current shell. In contrast, placing cd inside parentheses creates a subshell: even the builtin’s change stays in that child environment. What matters is not only the command, but the execution environment in which it runs. [1][2]

先做一個不寫檔的小觀察:在 Bash 依序輸入下方三行。中間的括號只在子 shell 切到根目錄並列印位置;最後一行則再次觀察外層目錄。本文依官方手冊解說,未把這段範例宣稱為本次 Linux 真機實測;若原本就在根目錄,前後自然看不出差異。

For a small observation that writes no files, enter the three lines below in Bash. The parenthesized commands change to the root directory and print it only in a subshell; the last line observes the outer directory again. This explanation follows the official manual, not a claimed Linux hardware test in this run. If you already started at root, naturally there will be no visible difference.

pwd
( cd / && pwd )
pwd

需要留下變更時,再決定怎麼執行 / Decide what should persist

若目的就是讓目前終端機切換目錄,可以直接輸入 cd,或定義在目前 shell 執行的函式。另一個選擇是用 source ./go.sh 讀入可信任的腳本,讓命令在目前 shell 執行;這不是單純「取得腳本結果」,而是讓腳本直接參與你的工作環境。[2]

To change the current terminal’s directory, use cd directly or define a function that runs in the current shell. Alternatively, source ./go.sh reads a trusted script and executes its commands in the current shell. This does not merely retrieve a script’s result: it lets the script participate directly in your working environment. [2]

因此,不要為了讓目錄留下來,就隨手讀入陌生腳本;它也可能改變變數、選項,甚至結束目前 shell。若只是想在別處完成一段工作,保留子 shell 反而清楚:進去工作,離開後不打擾原本位置。下次看到目錄「回來了」,先問是哪個 shell 在移動,而不是急著重寫路徑。

Do not source an unfamiliar script just to make a directory change persist: it may also change variables and options, or even exit the current shell. If you only need to do some work elsewhere, a subshell is a clear choice: enter, do the work, and leave the original location undisturbed. Next time a directory seems to “revert,” ask which shell moved before rewriting the path.

資料來源 / Sources

查核日期 / Checked: 2026-10-31。依官方文件整理;範例未宣稱本次 Linux 實測。 / Based on official documentation; examples are not claimed as Linux tests performed in this run.

  1. GNU Bash Reference Manual — Command Execution Environment
  2. GNU Bash Reference Manual — Bourne Shell Builtins (cd, . / source)

留言

這個網誌中的熱門文章

kimiflare:將 Moonshot Kimi-K2.6 直接部署於 Cloudflare 的終端 AI 編碼助手 | kimiflare: A Terminal Coding Agent Powered by Moonshot Kimi-K2.6 on Cloudflare

無需中繼,在你的 Cloudflare 帳戶中直接運行強大的 Kimi 終端編碼智能體。 | No middleman, run the powerful Kimi terminal coding agent directly on your own Cloudflare account. 🔎 工具速覽 / AT A GLANCE Category AI Coding Agent / Developer Tool Pricing Free (Open-weight model / Cloudflare Workers AI pricing) BestFor Developers seeking a private, high-context terminal AI assistant with direct infrastructure control. GitHub Stars ⭐ 36 🚀 引言 / Introduction kimiflare 是一款為開發者量身打造的終端編碼智能體,它將 Moonshot 的 1 兆參數開源模型 Kimi-K2.6 與 Cloudflare Workers AI 完美結合。不同於傳統 AI 工具,kimiflare 讓流量直接在你的 Cloudflare 帳戶中流動,徹底擺脫第三方代理或 AI 閘道的束縛,實現最高等級的控制力與隱私。 | kimiflare is a terminal coding agent tailored for developers, seamlessly combining Moonshot's 1T-parameter open-source model Kimi-K2.6 with Cloudflare Workers AI. Unlike traditional AI tools, kimiflare routes traffic directly through your Cloudflare account, eliminating the n...

text-to-cad: 讓 AI 驅動的參數化 CAD 建模成為現實 / text-to-cad: Bringing AI-Driven Parametric CAD Modeling to Reality

一個讓 AI 代理能生成、預覽並迭代 3D CAD 模型的開源框架 / An open-source harness for AI agents to generate, preview, and iterate 3D CAD models. 🔎 工具速覽 / AT A GLANCE Category AI-Powered CAD Tooling / AI 驅動 CAD 工具 Pricing Free (Open Source) / 免費(開源) BestFor Robotics engineers and product designers using AI coding agents / 使用 AI 程式碼代理的機器人工程師與產品設計師 GitHub Stars ⭐ 93 🚀 引言 / Introduction text-to-cad 是一個強大的開源框架,旨在填補大型語言模型(LLM)與專業 3D 建模之間的鴻溝。它允許開發者利用 Claude Code 或 Codex 等 AI 代理,透過程式碼生成精確的參數化幾何模型,並提供即時預覽與迭代能力。 text-to-cad is a powerful open-source harness designed to bridge the gap between Large Language Models (LLMs) and professional 3D modeling. It enables developers to leverage AI agents like Claude Code or Codex to generate precise parametric geometry via code, providing real-time preview and iteration capabilities. 🛠️ 核心功能 / Key Features 與 Codex 或 Claude Code 等代理...

Orca Skills: 開源賦能,加速雲安全工作流

加速雲安全工作流 - Orca Skills 整合多種工具與插件,打造高效安全環境 | Accelerate Cloud Security Workflows with Orca Skills, a Suite of Tools and Plugins 🔎 工具速覽 / AT A GLANCE Category 开源安全工具 Pricing 开源免费 BestFor 雲安全团队、DevSecOps工程师 GitHub Stars ⭐ 33 🚀 引言 / Introduction 在雲端環境中,確保安全不僅是一種技術挑戰,更是對工作效率的一場考驗。Orca Skills 是一款專為加速 Orca 云平台安全工作流而設計的开源插件工具包。透過整合多種工具和插件,幫助我們在複雜的安全環境下保持高效率。 🛠️ 核心功能 / Key Features Orca Skills 提供了一系列功能強大的安全工作流工具,包括警報處理、影響分析、配置源頭追溯等。這些工具不僅能夠幫助我們快速定位問題,還能確保在解決問題時避免造成不必要的後續問題。 💡 技術亮點 / Tech Highlights Orca Skills 真正的亮點在于其開放性與兼容性。它不僅支持Anthropic Claude Code、Cursor 和 Codex,還能夠與 MCP(Orca Security)集成,為安全團隊提供全面的支持。 📦 快速上手 / Quick Start 要快速開始使用 Orca Skills,首先需要安裝所需環境和工具。接下來,配置 MCP 以實現與 Orca Cloud Platform 的集成。最後,測試各個技能插件以確保其正常運行。 中英文對照: 要快速開始使用 Orca Skills 首先需要安裝所需環境和工具 接著配置 MCP 以實現與 Orca 云平台的集成 最...