bash處理一條命令的步驟

寫在前面

本文將大致闡述當我們在命令行輸入一條完整的命令後(敲了回車鍵),到該命令真正開始執行這段時間內bash對輸入的文本命令所做的諸多操作。

講解shell編程的書籍多如牛毛,卻很少有哪一本會完整地介紹這些操作以及說清楚它們的前後發生順序,都是比較簡單地或者說強硬地羅列知識點而已。這就會導致一個非常嚴重的問題,那就是導致很多學習者很快就會迷失在零散的知識海洋中。因爲不瞭解當前學習的知識點處於諸多操作的哪個階段,自然就不會了解應該注意什麼,更不會了解它與前後階段的區別與聯繫,這樣學習就不會觸類旁通,最終的學習效果也是不夠深入。

但是一旦你瞭解了shell在開始執行命令之前對我們輸入的命令所做的諸多操作,並瞭解他們的執行順序的話,心中就有了一個很清晰的脈絡,就會很自然地瞭解所學的知識點當前處於哪個操作階段,以及與前後階段的關係,以這樣的方式學習,我相信每個人都不會在零散的知識點裏迷失方向,達到事半功倍的效果。

 

命令行處理流程

如果我們查閱bash的在線手冊,就會發現在3.1.1小節已經對bash處理一條命令的流程做了一個整體的概括。摘錄如下:

3.1.1 Shell Operation
---------------------

The following is a brief description of the shell's operation when it
reads and executes a command.  Basically, the shell does the following:

  1. Reads its input from a file (*note Shell Scripts::), from a string
     supplied as an argument to the '-c' invocation option (*note
     Invoking Bash::), or from the user's terminal.

  2. Breaks the input into words and operators, obeying the quoting
     rules described in *note Quoting::.  These tokens are separated by
     'metacharacters'.  Alias expansion is performed by this step (*note
     Aliases::).

  3. Parses the tokens into simple and compound commands (*note Shell
     Commands::).

  4. Performs the various shell expansions (*note Shell Expansions::),
     breaking the expanded tokens into lists of filenames (*note
     Filename Expansion::) and commands and arguments.

  5. Performs any necessary redirections (*note Redirections::) and
     removes the redirection operators and their operands from the
     argument list.

  6. Executes the command (*note Executing Commands::).

  7. Optionally waits for the command to complete and collects its exit
     status (*note Exit Status::).

參考鏈接:

GNU Bash Manual

Learning the bash Shell, 3rd Edition  -- 7.3. Command-Line Processing

Processing the Command Line

SHELL(bash)腳本編程六:執行流程

窺豹一斑:通過$@看bash對一個簡單命令的預處理

shell 命令執行順序 一

shell 執行命令順序 二

Bash命令行處理[詳解] 

shell 命令行處理流程

靈異的shell

bash命令行處理步驟及源碼解析

詳解Bash命令行處理

bash命令行處理流程及關於雙引號的源碼解析

Mastering Bash and Terminal

Shell主要邏輯源碼級分析:SHELL 運行流程

卷1:第3章 The Bourne-Again Shell》《bash的展開

Shell:通過實例理解Bash命令替換和命令行處理流程

深入理解Linux Shell》《Bash命令行處理流程詳解》《Bash腳本語法泛述

Shell 參數擴展及各類括號在 Shell 編程中的應用

bash命令行處理步驟及源碼解析》《bash命令行解釋順序》《命令行處理

Shell:通過實例理解Bash命令替換和命令行處理流程

發佈了160 篇原創文章 · 獲贊 136 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章