processo de interpretação de linha bash

processo de interpretação de linha bash

Gostaria de entender o processo exato de interpretação de linha executado pelo bash.

do manual de referência do GNU bash:

When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.

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 (see Shell Expansions). 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 as described above (see Redirections).

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 any of the assignments attempts to assign a value to a readonly variable, an error occurs, and the command exits with a non-zero status.

If no command name results, redirections are performed, but do not affect the current shell environment. A redirection error causes the command to exit with a non-zero status.

If there is a command name left after expansion, execution proceeds as described below. Otherwise, the command exits. If one of the expansions contained a command substitution, the exit status of the command is the exit status of the last command substitution performed. If there were no command substitutions, the command exits with a status of zero.

agora vamos dar um exemplo simples:

var="hello friend"

echo ${var}s > log.txt

o que acontece agora?

de acordo com a referência, a atribuição de var (1) ocorrerá após as expansões (4), mas como o shell pode expandir a var sem realizar primeiro a atribuição de variável?

Não sei se é algo que perdi aqui ou apenas um mal-entendido do manual.

Agradecerei se você puder me dar exemplos adicionais para maior compreensão.

obrigado

Responder1

...expansões, atribuições e redirecionamentos, da esquerda para a direita.

O manual faznãomencione que ele também analisa dede cima para baixo, uma linha após a outra. Ele fala apenas sobrecomandos simples.

Você sempre pode mudar

cmd1
cmd2

em

cmd1; cmd2

Mas normalmente

com ma nd 1
co mma nd 2

é preferido pelos humanos

com ma nd 1; co mma nd 2

No bash você não tem =vs. ==, então é necessária essa sintaxe especial paraatribuições.Redirecionamentostambém são mencionados, e você pode colocá-los em qualquer lugar:

> log.txt echo ${var}s 
echo ${var}s>log.txt

Continuação da linhaé o contrário:

com \
mand 1

Responder2

Você tem 2 instruções simples, 1 por linha. Portanto, para a primeira linha, as etapas 1,2 e 3 não fazem nada, então a etapa 4 é a atribuição de variável.

Para a segunda linha, a variável é expandida na etapa 1.

Responder3

Não é a mesma linha. O shell seguirá essas etapas duas vezes.

Além disso, observe que estes:

var="hello friend"; echo ${var}s > log.txt

também são dois comandos simples. Mas isso:

varr="hello friend" echo ${varr}s > log.txt

é um comando simples.Neste caso sua dúvida se aplica: ${varr}irá expandir para uma string vazia (a menos que tenha sido atribuída anteriormente; usei deliberadamente um novo nome, para que a atribuição antiga não interfira).

informação relacionada