当前位置 博文首页 > astrotycoon: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命令替换和命令行处理流程》
cs