Invalid class name

Fix "Invalid class name" error in Linux/Unix scripts

Linux & Unix Beginner 👁 0 views 📅 May 25, 2026

This error shows up in Bash or Python scripts when a variable or function name breaks naming rules. Here's how to spot and fix it fast.

Quick answer: Rename your variable, function, or class so it starts with a letter or underscore, contains only letters, numbers, and underscores, and avoids any reserved keywords like if, while, or class.

Why this happens

The "Invalid class name" error usually hits when you're writing a Bash script, a Python script, or sometimes a Perl or AWK script. It's not a kernel panic or a hardware fault — it's the interpreter refusing to accept a name that breaks its rules.

I've seen this trip up beginners who name a variable 1st_choice (starts with a number) or my-var (has a dash). In Python, calling a class class or a function return will also throw this error because those are reserved words. The interpreter can't tell if you're defining a new thing or referencing the built-in command.

One real-world trigger: someone writes a Bash script to parse log files and names a variable if inside a loop. The shell sees if and tries to start a conditional statement instead of assigning a value. Boom — "Invalid class name".

How to fix it (step by step)

  1. Find the exact line causing the error. Run your script again and look at the error message. It usually says something like line 12: invalid class name. Note that line number.
  2. Open the script in a text editor. Use nano, vim, or whatever you prefer. Go to the line number from step 1.
  3. Check the name that's being defined. Look for a variable assignment (like my-var=5), a function definition (like function 123func), or a class definition (like class while).
  4. Make sure the name starts with a letter or underscore. For example, change 1st_choice to first_choice or _1st_choice. Change 123func to func123.
  5. Replace any hyphens or special characters with underscores. The only valid character besides letters and numbers is the underscore (_). So my-var becomes my_var, and data.point becomes data_point.
  6. Avoid using reserved keywords as names. In Bash, that includes if, then, else, fi, while, do, done, for, in, case, esac, function, select, until. In Python, it's False, None, True, and, as, assert, class, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield. If you used one of these, rename it to something else like my_class or condition_flag.
  7. Save the file and re-run the script. After applying the rename, run bash script.sh or python3 script.py again. The error should be gone. If it's not, check for other lines with the same problem.

Alternative fixes if the main one doesn't work

  • Check for hidden whitespace. Sometimes a line looks like my_var=5 but has a tab or space before it. In Bash, that can make the interpreter read the name differently. Run cat -A script.sh to see if there are ^I (tabs) or trailing spaces $ that shouldn't be there. Remove them.
  • Look at the shebang line. If your script is supposed to run in Bash but the first line says #!/bin/sh, it might use a more restrictive shell. Change it to #!/bin/bash and test again.
  • Check for mismatched quotes. A missing closing quote can make the interpreter read the next line as part of the same string, causing the name to look like my_var=hello world (with a space). That's invalid. Fix the quoting.
  • If you're using Python with a class definition that includes inheritance, make sure the parent class name is valid, too. For example, class MyClass(123base): will fail because 123base starts with a number. Rename it to something like BaseClass123.

How to prevent this in the future

Follow these naming rules from the start and you'll never see this error again:

  • Use only letters, numbers, and underscores in names.
  • Start every name with a lowercase letter or underscore — never a number.
  • Don't use any reserved keyword as a name. Keep a list handy or use an IDE that highlights them.
  • In Bash scripts, avoid naming functions with hyphens or dots. It's common to see get-data but that won't work — use get_data instead.
  • Use comments to document what each variable or function does. That way you're less likely to reuse a keyword by accident.

That's it. Most of the time this error is just one wrong character away from a fix. Once you know the rules, you'll spot bad names before the interpreter does.

Was this solution helpful?