-2

Can a function only be called if its name is the first word of an expression, as is the case with aliases?
Or can it also be the nth word of an expression?
I can't emulate a use case that answers this question of mine.
I can't find a guide that answers this question of mine.
Being Italian, I find it difficult to find the right words on the Google search engine.
Can you also mention the source in the answer?
Thank you.

Update:
5 answers (including 1 deleted and 2 non-answers) despite the question received 2 unjustified negative votes. A question simple to understand, complete as it is and that does not need details, which if inserted anyway only confuse the concept. Despite my dissatisfaction with users' reactions and unkind comments, the @muru answer is the most correct one. Obviously things change in a for loop but it was implied, I've used bash for years too.
Your link answers my question: Function Definition Command, where there is a point that says:
"
The compound-command shall be executed whenever the function name is specified as the name of a simple command.
The operands to the command temporarily shall become the positional parameters during the execution of the compound-command; the special parameter '#' also shall be changed to reflect the number of operands. The special parameter 0 shall be unchanged. When the function completes, the values of the positional parameters and the special parameter '#' shall be restored to the values they had before the function was executed. If the special built-in return (see return) is executed in the compound-command, the function completes and execution shall resume with the next command after the function call.
"
Thank you @muru.

6
  • 2
    How is myfunction any different from the arguments to anycommand? What do you want to accomplish exactly?
    – muru
    Oct 26, 2020 at 9:09
  • This is a generic question, perhaps the additional details confused the question instead of making it clearer. The question is simple, I want to know if a function can only be called if its name is the first word of an expression or not, citing the source that writes it. Oct 26, 2020 at 9:12
  • In the expression, x=y z, the function z is called, but it is the second word.
    – xiota
    Oct 29, 2020 at 10:11
  • In this example, eval z, the function z is again the second word. It is called without any expansion or removal of other words or fields for processing. The expression can be made arbitrarily long: eval eval eval eval eval z. It demonstrates that functions do not have to be the first word in an expression to be called, though I don't see any purpose for it.
    – xiota
    Oct 29, 2020 at 12:16
  • Please, can you elaborate the question? what is it you're trying to achieve? For example if I have a function Function1 and there already is a function called Function1Function1 - of course calling Function1 would work; but I am not sure I follow your question? what is the goal? Can you provide a example of what you would want to do? Oct 30, 2020 at 14:20

4 Answers 4

1
+50

See POSIX specification of Shell Commands:

A command is one of the following:

  1. Simple command (see Simple Commands)
  2. ...

And in 2.9.1 Simple Commands, emphasis mine:

When a given simple command is required to be executed [...]:

  1. The words that are recognized as variable assignments or redirections according to Shell Grammar Rules are saved for processing in steps 3 and 4.

  2. The words that are not variable assignments or redirections shall be expanded. If any fields remain following their expansion, the first field shall be considered the command name and remaining fields are the arguments for the command.

...

If there is a command name, execution shall continue as described in Command Search and Execution. ...

Command Search and Execution

If a simple command results in a command name and an optional list of arguments, the following actions shall be performed:

  1. If the command name does not contain any characters, the first successful step in the following sequence shall occur:

    a. If the command name matches the name of a special built-in utility, that special built-in utility shall be invoked.

    ...

    c. If the command name matches the name of a function known to this shell, the function shall be invoked as described in Function Definition Command. ...

That last point is pretty much the only place which specifies the execution of a function. Everything else essentially comes to this. So, yes, a function name has to be the first field (after various expansions have been performed).

11
  • This answer is incorrect because it answers the question in the affirmative. However, it can be shown to be false by counter example. If z is a function, it can be called without being the first word in this expression: for i in x ; do y ; z ; done. Note: There is nothing wrong with the quotes from specification. It is the interpretation and the way it's phrased that is misleading. (eg, there is a switch in terminology in the specification that is not explained and carried into the summary)
    – xiota
    Oct 29, 2020 at 9:57
  • Another example: x=y zz is called, but it is the second word.
    – xiota
    Oct 29, 2020 at 10:06
  • What is an expression? As to the example, z is the second word, but the first field (<- the standard's choice of words) after removing variable assignments and redirections. By their own admission, OP lacks the knowledge of terminology required, so their "word" might or might not be what the standard calls a "field", and OP certainly hasn't defined a word or an expression.
    – muru
    Oct 29, 2020 at 10:23
  • The standard also refers to "words". You do not explain why you think the OP's use of "word" should refer to the standard's use of "field", rather than the standard's use of "word". By stating "yes" in your answer, you imply that the statement in the question, along with the terminology, is correct. Even so, your summative statement is incorrect as stated because it omits mention that words and fields may be removed prior to determining the command name. ("If any fields remain ...")
    – xiota
    Oct 29, 2020 at 11:43
  • Your assertion about me implying that obviously false, since you yourself say that I think OP means field when they say word, which obviously means that I think their terminology is incorrect.
    – muru
    Oct 29, 2020 at 11:44
0

Yes, a function can only be called by putting the function name as the first word of an expression. The following words are the arguments to the function. A function in bash works the same way as any other command or alias. A function name cannot be the nth word of the expression.

I understand what you are saying because my language is also not English. (In my language we can also turn a statement into a question by changing the intonation and adding a question mark, but in English, we strangely need to rearrange the words to make it into a question??)

2
  • In this expression, for i in x ; do y ; z ; done, z is not the first word, but could be called if it were a function. There are numerous other examples, x && y && z, x=y z, etc.
    – xiota
    Oct 27, 2020 at 12:51
  • Thanks for pointing that out, xiota. I was only thinking of the function call itself; I wasn't thinking about the function call being part of a larger command.
    – bamm
    Oct 29, 2020 at 10:35
-2

Aliases are expanded only on the first word of the command, not in the parameters of the command.

Read man bash.

alias foo=`ls;rm *;`
...
echo I said foo again

Is the above what you seek?

That's not how bash works, and would make it unusable.

2
  • This doesn't answer my question, which isn't about aliases but about functions. Oct 24, 2020 at 17:28
  • Is the above what you seek? No Oct 26, 2020 at 9:09
-2

Use the bash $() construct.

command arg1 $(function function_args)
3
  • Could you explain this?
    – xiota
    Oct 27, 2020 at 12:58
  • Is not the my question yet. Oct 27, 2020 at 14:42
  • I didn't ask how to solve a problem but a simple answer to a question. Nov 1, 2020 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.