“running a function definition” and “running the definition of a function” are not common idioms. I expect that most people would refer to that simply as “defining a function”. OK, that phrase might be interpreted to refer to
- conceptually specifying the characteristics and interfaces (parameters and other inputs, processing, and outputs) of a function (this is an activity performed by a person, or a group of people)
- choosing the implementation of a function; i.e., what commands to use in the function body. This is an activity performed by a person, or a group of people, possibly using paper, whiteboards, and/or blackboards.
- typing the implementation of a function into a text editor or directly into an interactive shell (this, of course, is also an activity performed by a person, or possibly a group of people, or possibly a very intelligent and dexterous cat, dog or monkey)
If you fear confusion with the above, you might be better served by phrases like “reading a function definition” and “processing the definition of a function”.
You say, “A function definition is a command.” It’s not clear what a claim like that even means — it’s a matter of semantics — but, at the semantic level, it’s debatable. Section 3.2 of the Bash manual (Shell Commands) lists six kinds of shell commands: Simple Commands, Pipelines, Lists, Compound Commands, Coprocesses, and GNU Parallel. Function definitions don’t fit well into any of those categories.
Переходим к вашему вопросу и рассматриваем семь шагов / операций,
Анализ токенов. Это должно хотя бы начаться на этапе «обработка определения функции» чтобы оболочка понимала , что она просматривает определение функции и не простая команда. Кроме того, мы можем провести этот простой эксперимент :введите в оболочку одно из следующих действий:
myfunc1() { myfunc2() {
< or ;
} }
Произойдет сбой с «синтаксической ошибкой». прежде чем вы получите возможность ввести }
.(>
и &
дают тот же эффект. )Таким образом, очевидно, что этот шаг является частью обработки определения функции.
echo $var
(, который должен быть echo "$var"
,но это другое дело от )до echo *
. echo *
и не был расширен. cd
)и/или создать файл (s )и/или удалить файл (с )и/или переименовать файл (с ), а затем вызовите (выполните )функцию. Вы увидите, что он выводит текущий список файлов 1 в текущем каталоге, и не отражает содержимое каталога где/когда вы определили функцию. echo $var
на echo hello > myfile
. > myfile
. myfile
еще не создано (). Другими словами, именно то, что сказал @ilkkachu .
Единственный исключительный частный случай, о котором я могу думать заключается в том, что шаги 2 и 3 (лексический анализ и синтаксический анализ )повторяются (снова )при выполнении функции, если функция содержит операторы eval
— но я считаю, что это выходит за рамки того, что ваш вопрос действительно спрашивает.
____________
1 исключая скрытые (точечные -)файлы, если вы не сказалиshopt -s dotglob