首页 » 信息技术 »

Bash的命令搜索和执行

2019年2月15日 / 14次阅读
Bash

  • 打开支付宝,搜索“ 529018372 ”,领取专属红包!每日支付每日领。

After a command has been split into words, if it results in a simple command and an optional list of arguments, the following actions are taken.

命令首先被分割成每个单词之后。(空格在Bash中有特殊含义,所以变量赋值不能有空格)

If the command name contains no slashes, the shell attempts to locate it. If there exists a shell function by that name, that function is invoked as described in Shell Functions.

命令不能有\符号,这表示命令还没写完。

首先,Bash查找Function。

If the name does not match a function, the shell searches for it in the list of shell builtins. If a match is found, that builtin is invoked.

然后,Bash查找builtins命令。

If the name is neither a shell function nor a builtin, and contains no slashes, Bash searches each element of $PATH for a directory containing an executable file by that name. Bash uses a hash table to remember the full pathnames of executable files to avoid multiple PATH searches (see the description of hash in Bourne Shell Builtins). A full search of the directories in $PATH is performed only if the command is not found in the hash table.If the search is unsuccessful, the shell searches for a defined shell function named command_not_found_handle. If that function exists, it is invoked in a separate execution environment with the original command and the original command’s arguments as its arguments, and the function’s exit status becomes the exit status of that subshell. If that function is not defined, the shell prints an error message and returns an exit status of 127.

最后,才到PATH中的路径去找外部程序。

Bash会给外部命令建立一个Hash表,加快后续查找命令的速度。

If the search is successful, or if the command name contains one or more slashes, the shell executes the named program in a separate execution environment. Argument 0 is set to the name given, and the remaining arguments to the command are set to the arguments supplied, if any.

If this execution fails because the file is not in executable format, and the file is not a directory, it is assumed to be a shell script and the shell executes it as described in Shell Scripts.

If the command was not begun asynchronously, the shell waits for the command to complete and collects its exit status.

默认Bash采用同步的方式执行命令,即要等待命令执行结束之后,收集到了命令执行结束的exit status,再响应下一条命令。

本文链接:http://www.maixj.net/ict/bash-cmd-20145

相关文章

评论是美德

《Bash的命令搜索和执行》有1条评论

  • 麦新杰

    去掉bash的h属性(hashall,set +h),修改PATH中路径的前后顺序,就可以改变bash调用程序的位置。如果有hashall属性,bash第一次调用的程序,将会在后续过程中继续调用,位置已经被记录在它自己的hash表里面了。 []


前一篇:
后一篇:

栏目精选

云上小悟,麦新杰的独立博客

Ctrl+D 收藏本页

栏目


©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1。云上小悟网站部分内容来源于网络,转载目的是为了整合信息,收藏学习,服务大家,有些转载内容也难以判断是否有侵权问题,如果侵犯了您的权益,请及时联系站长,我会立即删除。

网站二维码
go to top