Bash-Zeileninterpretationsprozess

Bash-Zeileninterpretationsprozess

Ich möchte den genauen Prozess der Zeileninterpretation verstehen, der von Bash ausgeführt wird.

aus dem GNU Bash-Referenzhandbuch:

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.

Nehmen wir nun ein einfaches Beispiel:

var="hello friend"

echo ${var}s > log.txt

Was passiert jetzt?

gemäß der Referenz erfolgt die Variablenzuweisung (1) nach der Erweiterung (4), aber wie kann die Shell die Variable erweitern, ohne vorher die Variablenzuweisung durchzuführen?

Ich weiß nicht, ob ich etwas übersehen habe oder ob ich das Handbuch einfach missverstanden habe.

Ich wäre Ihnen dankbar, wenn Sie mir zum besseren Verständnis weitere Beispiele geben könnten.

Danke

Antwort1

...Erweiterungen, Zuweisungen und Umleitungen, von links nach rechts.

Das Handbuchnichterwähnen Sie, dass es auch analysiert vonoben nach unten, eine Zeile nach der anderen. Es geht nur umeinfache Befehle.

Du kannst dich jederzeit ändern

cmd1
cmd2

hinein

cmd1; cmd2

Aber normalerweise

com ma nd 1
co mma nd 2

wird von Menschen bevorzugt

com ma nd 1; co mma nd 2

In Bash gibt es kein =vs. ==, daher ist diese spezielle Syntax fürZuordnungen.Umleitungenwerden ebenfalls erwähnt und diese können Sie überall einfügen:

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

Zeilenfortsetzungist umgekehrt:

com \
mand 1

Antwort2

Sie haben zwei einfache Anweisungen, eine pro Zeile. In der ersten Zeile bewirken die Schritte 1, 2 und 3 also nichts, dann ist Schritt 4 die Variablenzuweisung.

Für die zweite Zeile wird die Variable in Schritt 1 erweitert.

Antwort3

Es ist nicht dieselbe Zeile. Die Shell führt diese Schritte zweimal aus.

Beachten Sie außerdem Folgendes:

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

sind auch zwei einfache Befehle. Aber dieser:

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

ist ein einfacher Befehl.In diesem Fall trifft Ihr Zweifel zu: ${varr}wird zu einer leeren Zeichenfolge erweitert (es sei denn, sie wurde zuvor zugewiesen; ich habe absichtlich einen neuen Namen verwendet, damit die alte Zuweisung nicht stört).

verwandte Informationen