Bash ライン記述プロセス

Bash ライン記述プロセス

bashが行解析を実行する正確なプロセスを理解したいと思います。

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.

それでは簡単な例を挙げましょう。

var="hello friend"

echo ${var}s > log.txt

今何が起こるの?

参照によると、var割り当て(1)は拡張(4)の後に発生しますが、シェルは最初に変数割り当てを実行せずにvarを拡張する方法は?

これが私がここで逃した部分なのか、それともマニュアルの誤解なのかはわかりません。

より理解を深めるために、より多くの例をお寄せいただきありがとうございます。

ありがとう

ベストアンサー1

...左から右に展開、割り当て、およびリダイレクトします。

実際にはマニュアルに記載されています。いいえまた、次のように構文解析すると言及しました。上から下へ、一行ずつ。ただ物語簡単なコマンド

いつでも変更できます。

cmd1
cmd2

入力する

cmd1; cmd2

しかし、一般的な状況では

com ma nd 1
co mma nd 2

人間が好むもの

com ma nd 1; co mma nd 2

=Bashにはvs.がないため、==この特別な構文が必要です。仕事リダイレクトまた、次の内容をどこにでも入れることができます。

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

行連続その逆:

com \
mand 1

おすすめ記事