proceso de interpretación de línea bash

proceso de interpretación de línea bash

Me gustaría entender el proceso exacto de interpretación de líneas ejecutado por bash.

del manual de referencia de 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.

ahora tomemos un ejemplo simple:

var="hello friend"

echo ${var}s > log.txt

¿que pasa ahora?

Según la referencia, la asignación de var (1) se llevará a cabo después de las expansiones (4), pero ¿cómo puede el Shell expandir la var sin realizar primero la asignación de variable?

No sé si es algo que me perdí aquí o simplemente una mala comprensión del manual.

Le agradecería que pudiera darme ejemplos adicionales para una mayor comprensión.

gracias

Respuesta1

...expansiones, asignaciones y redirecciones, de izquierda a derecha.

el manual hacenomencionar que también analiza desdede arriba hacia abajo, una línea tras otra. solo habla decomandos simples.

siempre puedes cambiar

cmd1
cmd2

en

cmd1; cmd2

Pero normalmente

com ma nd 1
co mma nd 2

Es preferido por los humanos a

com ma nd 1; co mma nd 2

En bash no tienes =vs. ==, por lo que se necesita esta sintaxis especial paraasignaciones.RedireccionesTambién se mencionan, y estos puedes ponerlos en cualquier lugar:

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

Continuación de líneaes al revés:

com \
mand 1

Respuesta2

Tienes 2 declaraciones simples, 1 por línea. Entonces, para la primera línea, los pasos 1, 2 y 3 no hacen nada, luego el paso 4 es la asignación de variables.

Para la segunda línea, la variable se expande en el paso 1.

Respuesta3

No es la misma línea. El shell seguirá estos pasos dos veces.

Además tenga en cuenta que estos:

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

También hay dos comandos simples. Pero esto:

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

es un comando simple.En este caso tu duda aplica: ${varr}se expandirá a una cadena vacía (a menos que se haya asignado antes; usé deliberadamente un nombre nuevo, para que la asignación anterior no interfiera).

información relacionada