简体   繁体   English

Linux bash/shell 是如何读取我们给它的命令的?

[英]How does Linux bash/shell read the command we give to it?

For example, if I send bash -c " echo "$S" "例如,如果我发送bash -c " echo "$S" "

How does it read the command, from left to right, or read from left and right together to middle?它如何读取命令,从左到右,或从左和右一起读取到中间? or else?要不然?

If it's not too much, can you list a step by step, like how the linux bash/shell read and treat every single character and symbol it receives?如果不是太多,您能否逐步列出,例如 linux bash/shell 如何读取和处理它接收到的每个字符和符号?

Assuming that you are issuing this command from a bash shell, and the variable S contains the value xy :假设您从bash shell 发出此命令,并且变量S包含值xy

First, the variable is expanded, which would leave you with首先,变量被扩展,这会给你留下

bash -c " echo "x      y" " 

Then, the quotes are expanded.然后,引号被扩展。 Therefore, you are left with 4 token:因此,您剩下 4 个令牌:

  • bash bash
  • -c -C
  • echo x (one leading space) echo x (一个前导空格)
  • y (ie y followed by one space) y (即 y 后跟一个空格)

Next, another bash is started, with the arguments -c , echo x and y .接下来,启动另一个 bash,其中包含 arguments -cecho xy

This bash-subshell would now execute the echo command and print xy .这个 bash-subshell 现在将执行 echo 命令并打印xy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM