Bash - Loading environment variables from a file
Learn how to load environment variables from a .env file into a Bash script, so you can externalize configuration and reuse variables across other applications like Laravel.
This article is over a year old but was reviewed on July 30, 2026 — the content is still accurate.
This article is part of the Writing better Bash scripts series:
- Bash - ASCII art
- Bash - Loading environment variables from a file
- Bash - Script to add logging features to your script
- Bash - Echo on the console and in a logfile in the same time
- Linux - Compare two versions of the same script
- Linux - Sort functions in a Bash script
- Linux - Generate documentation from Bash scripts
- Linux - Take advantage of the number of CPUs you have; start concurrent jobs
- Linux - Using a progression bar in your script
- Linux - Bash scripts - Running unit tests with bats/bats

This article shows the correct way to load a .env file's variables into a Bash script's environment: set -o allexport; source .env; set +o allexport, which handles values containing spaces correctly — unlike the common but unreliable export $(... | xargs) pattern.
Imagine you have a .env file like this one and you wish to process that file in a Bash script.
DOCKER_GIT_USEREMAIL="christophe@me.com"DOCKER_GIT_FULLNAME="Christophe Avonture"DOCKER_GIT_USERNAME="Me and myself"This script snippet defines three environment variables that store Git configuration information, likely to be used inside a Docker container. These variables capture the user's email, full name, and username so they can be automatically configured when Git is initialized in the container.
Using a configuration file will enable you to externalize the management of your constants, as well as reuse variables from another application, such as a site developed in Laravel.
Let's take a look at how to do this as correctly as possible.
The snippet comes from https://gist.github.com/mihow/9c7f559807069a03e302605691f85572?permalink_comment_id=3954807#gistcomment-3954807
You can load that file easily in your environment using the following instructions:
set -o allexport
source .env
set +o allexport
This done, variables will be accessible like any environment variables in your bash script. When the script exits, the added variables are removed (just like in a sub-shell).
Using source is the best solution to avoid problems with f.i. spaces like in Me and myself i.e. using other solutions like export $(... | xargs) will always give unpredictable results.
To illustrate this, simply create a file called f.i. test.sh with this content:
#!/usr/bin/env bash
set -o allexportsource .envset +o allexport
echo "${DOCKER_GIT_FULLNAME} (${DOCKER_GIT_USEREMAIL})"This script loads environment variables from a .env file and then prints out two of those variables (a full name and email address). It's a simple way to read configuration from a file without polluting the shell's environment with temporary variables.
Using the .env file provided here above, we'll obtain this output:
This is, I think, the best and, too, the easiest way to use an external file to store configuration items in Bash.
Two more .env companions from this blog: Compare environment files in the Linux console, to spot the key you forgot to report from .env.example, and Batch edit of environment file, to change the same variable in a dozen projects at once.
This article is part of the Writing better Bash scripts series:
- Bash - ASCII art
- Bash - Loading environment variables from a file
- Bash - Script to add logging features to your script
- Bash - Echo on the console and in a logfile in the same time
- Linux - Compare two versions of the same script
- Linux - Sort functions in a Bash script
- Linux - Generate documentation from Bash scripts
- Linux - Take advantage of the number of CPUs you have; start concurrent jobs
- Linux - Using a progression bar in your script
- Linux - Bash scripts - Running unit tests with bats/bats





