0

Here's a script that throws a syntax error if and only if the function is named in:

in() {
        echo 0;
}

line 1: syntax error near unexpected token `in'

Why is that so? I cannot give this function a different name, so what should I do?

5
  • @zevzek The actual script is split into several sub command, and "in" is one of them. I'm using rici's solution from this thread: stackoverflow.com/questions/13638248/sub-commands-with-bash. With his solution, the backslash is not requires. Thanks, I just needed to declare it, but I'm still wondering why in() { } wouldn't work. Commented Oct 19, 2021 at 2:48
  • This might help: type in
    – Cyrus
    Commented Oct 19, 2021 at 2:51
  • Try to get a basic understanding of syntax, identifiers and keywords. In most languages you cannot use keywords for identifiers. Guess what could happen if you'd name your function if or return.
    – U. Windl
    Commented May 4, 2023 at 8:00
  • @U.Windl I mean no disrespect, but please take a look at the question's redaction date and the other replies. Commented May 5, 2023 at 11:09
  • @B.Bergeron What is your concrete criticism? That I repeat what someone else had said before, or that I responded late? Anyone can find a question or answer years after it had been written. If posters just seek for a quick response to their actual needs, SO/SE is probably the wrong place to ask.
    – U. Windl
    Commented May 9, 2023 at 7:43

1 Answer 1

2

in is a bash keyword and is reserved. You can't use it. Read man bash.

3
  • Open man bash and type /in. man bash is a bad resource for quick references. Can you instead explain what's this in keyword and how to deal with it? Commented Oct 19, 2021 at 2:59
  • in is often used with for. e.g. for i in 1 2 3 4 5; do echo "$i"; done. Type help for in bash.
    – cas
    Commented Oct 19, 2021 at 3:25
  • 2
    Refer to "RESERVED WORDS" in man bash.
    – cutrightjm
    Commented Oct 19, 2021 at 3:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .