3.7ExecutingCommands

Simple Command Expansion

When a simple command is executed, the shell performs the following expansions, assignments, and redirections,
from left to right, in the following order.
1.The words that the parser has marked as variable assignments(those preceding the command name) and redirections
are saved for later processing.
2.The words that are not variable assignments or redirections are expanded.
If any words remain after expansion, the first word is taken to be the name of the command and the remaining
words are the arguments.
3.Redirections are performed.
4.The text after the ‘=in each variable assignment undergoes tilde expansion, parameter expansion,
command substitution, arithmetic expansion, and quote removal before being assigned to the variable.

If no command name results,the variable assignments affect the current shell environment.
Otherwise,the variables are added to the environment of the executed command and do not affect the current shell environment.

If no command name results,redirections are performed,but do not affect the current shell environment.

例子

>123#会在当前目录下生成一个名为123的文件

./test
#./test: error while loading shared libraries: libmymath.so: cannot open shared object file: No such file or directory
LD_LIBRARY_PATH=./ ./test
#14+7=21
#14-7=7
#14/7=2
./test
#./test: error while loading shared libraries: libmymath.so: cannot open shared object file: No such file or directory
echo ${LD_LIBRARY_PATH}#output nothing

#The shell expands the variable in the command line before it actually runs the command.
#It outputs nothing,because $TST会先进行expansion
TST=foo echo $TST
#It works,because TST没有加$号,所以没有expansion
TST=foo printenv TST

Command Search and Execution

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.

1.#命令不包含/,先判断是否为shell function.
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.

2.#命令不包含/,再判断是否为shell builtin.
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.

3.#命令不包含/,再从PATH环境变量中查找.
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使用hash table来记录,避免每次都从PATH中去找
Bash uses a hash table to remember the full pathnames of executable files to avoid multiple PATH searches.
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 is not defined, the shell prints an error message and returns an exit status of 127.

4.#命令包含/时,直接执行
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.

5.#如果文件不是一个ELF程序,也不是一个目录,就被认为是shell脚本去执行.
#(比如将一个文本文件赋予可执行权限,在bash中执行时,会被当做shell脚本文件执行)
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.

6.#如果命令不是异步的,shell进程等到该命令完成.
If the command was not begun asynchronously, the shell waits for the command to complete and collects its exit status.

例子

root@kafka100:~/workspace/cpp #greg
bash: greg: command not found
root@kafka100:~/workspace/cpp #echo $?
127

Command Execution Environment

When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate
execution environment that consists of the following.
Unless otherwise noted, the values are inherited from the shell.
1.the shell’s open files, plus any modifications and additions specified by redirections to the command
2.the current working directory
3.the file creation mode mask
4.shell variables and functions marked for export, along with variables exported for the command, passed in the environment.

A command invoked in this separate environment cannot affect the shell’s execution environment.

A subshell is a copy of the shell process.
Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment
that is a duplicate of the shell environment.
Builtin commands that are invoked as part of a pipeline are also executed in a subshell environment.
Changes made to the subshell environment cannot affect the shell’s execution environment.

Environment

When a program is invoked it is given an array of strings called the environment.
This is a list of name-value pairs, of the form name=value.

Bash provides several ways to manipulate the environment.
On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking
it for export to child processes.
Executed commands inherit the environment.
The export and ‘declare -x’ commands allow parameters and functions to be added to and deleted from the environment.

The environment inherited by any executed command consists of the shell’s initial environment, whose values may be
modified in the shell, less any pairs removed by the unset and ‘export -n’ commands, plus any additions via the export
and ‘declare -x’ commands.

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter
assignments, as described in Shell Parameters.
These assignment statements affect only the environment seen by that command.

When Bash invokes an external command, the variable ‘$_’ is set to the full pathname of the command and passed to
that command in its environment.

https://www.gnu.org/software/bash/manual/bash.html#Command-Search-and-Execution
https://unix.stackexchange.com/questions/56444/how-do-i-set-an-environment-variable-on-the-command-line-and-have-it-appear-in-c
https://superuser.com/questions/801822/in-bash-how-do-i-define-a-command-scope-variable
https://unix.stackexchange.com/questions/222913/bash-shell-vs-environment-variable
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值