Coding⏱️ 4 min read📅 2026-06-19

How to Fix: Why does this bash script give an error on Ubuntu but not OSX

Error in bash script on Ubuntu but not OSX due to environment variable issue.

Quick Answer: Check for spaces around the assignment operator (=) and ensure correct path formatting.

The error message 'export: : bad variable name' is encountered when running the bash script on Ubuntu, indicating that the shell is unable to recognize the export command due to an invalid variable name. This issue affects users who want to run their scripts using cron and set environment variables.

This frustrating error can be caused by a variety of factors, including incorrect syntax, missing shebang lines, or issues with the shell's configuration. In this guide, we will explore the root causes of this error and provide two primary fix methods to resolve the issue.

🛑 Root Causes of the Error

  • The first main reason why this error happens is due to the use of single quotes instead of double quotes around variable names in the export command. In bash, single quotes are used to enclose string literals, while double quotes are used to enclose variable names and expansions. When a single quote is encountered within a double-quoted string, it can cause the shell to interpret the rest of the string as a separate command, resulting in an error.
  • An alternative reason for this error could be due to the presence of spaces around the equals sign (=) in the export command. Although you have confirmed that there are no spaces, it is still possible that the issue lies elsewhere, such as in the shell's configuration or in the script itself.

✅ Best Solutions to Fix It

Fix Method 1: Using Double Quotes

  1. Step 1: To fix this error, modify the export command to use double quotes around the variable name. Replace the line `export $NODE_CONFIG_DIR=/Users/full/path/to/script/config` with `export NODE_CONFIG_DIR='/Users/full/path/to/script/config'`. This will ensure that the shell recognizes the variable name and expands it correctly.
  2. Step 2: Save the changes to your script and test it again to verify that the error is resolved. If you are still experiencing issues, check your shell configuration files (e.g., ~/.bashrc) for any settings that may be interfering with the export command.

Fix Method 2: Escaping Single Quotes

  1. Step 1: As an alternative solution, you can use single quotes to enclose the variable name and escape any existing single quotes within it. Replace the line `export $NODE_CONFIG_DIR=/Users/full/path/to/script/config` with `export NODE_CONFIG_DIR='/Users/full/path/to/script/config'`. This will ensure that the shell interprets the single quotes correctly and expands the variable name as expected.
  2. Step 2: Save the changes to your script and test it again to verify that the error is resolved. If you are still experiencing issues, check your shell configuration files (e.g., ~/.bashrc) for any settings that may be interfering with the export command.

💡 Conclusion

To summarize, the 'export: : bad variable name' error can be caused by a variety of factors, including incorrect syntax or missing shebang lines. By using double quotes around variable names and escaping single quotes within them, you can resolve this issue and successfully run your bash script on Ubuntu. Remember to test your changes thoroughly and check your shell configuration files for any potential issues.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions