Software⏱️ 2 min read📅 2026-05-31

How to Fix: BASH script to set environment variables not working

Environment variables not set in BASH script.

Quick Answer: The issue is that the export statements are only setting the variables for the current shell session, but not persisting them across subsequent shells. To fix this, use the `source` command to run the script again in the current shell, or add a shebang line at the end of the script and make it executable.

The issue you're facing is due to the way BASH handles environment variables. When you use `export` in a script, it only sets the variable for the duration of the shell session. Once the script finishes executing, the variable is lost.

🛑 Root Causes of the Error

  • The `export` command only sets the variable for the current shell session.

🚀 How to Resolve This Issue

Method 1: Using `source` instead of `export'

  1. Step 1: Replace `export` with `source` in your script.

Method 2: Using `alias` or `function` to create a persistent variable

  1. Step 1: Instead of using `export`, define an alias or function that sets the variable.

💡 Conclusion

By following these methods, you can ensure that your environment variables are set persistently across multiple shell sessions.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions