When working with environment variables in a .env file, there are several methods for loading these variables into your shell session. Two common constructs you may come across in shell scripts or command lines are:

source .env

and

export $(cat .env | xargs)

While both accomplish similar tasks—loading environment variables—they do so in different ways, and each has its own strengths and potential drawbacks. In this article, we'll break down the differences between them to help you choose the best approach for your needs.

Method 1: source .env

The source command (or its equivalent, .) is a way to execute the contents of a file in the current shell. When you use source .env, it reads each line of the .env file as if you had typed it directly into your shell. This means that any KEY=VALUE pairs in the file are set as environment variables in your current shell session.

Example .env file

# .env file
API_KEY=your_api_key
DATABASE_URL=your_database_url
DEBUG=true

Running source .env

$ source .env
$ echo $API_KEY
your_api_key

After running source .env, API_KEY, DATABASE_URL, and DEBUG are now accessible as environment variables in the current shell session.

Pros of using source .env

  1. Simplicity: This command is easy to use and works well if your .env file has a straightforward format.
  2. Compatibility: It’s compatible with variables that use standard shell syntax and even allows for basic shell commands in .env files, as source simply executes each line.

Cons of using source .env

  1. Shell Dependency: The .env file must be formatted in a way that the shell can interpret directly. For example, export statements or commands with special characters may cause errors.
  2. Less Flexibility: Since source runs in the current shell, it won’t work for scenarios where you need to load the variables into a sub-shell or isolated environment.

Method 2: export $(cat .env | xargs)

This method combines several shell commands to accomplish a similar result, but with a slightly different approach. Breaking it down:

  • cat .env reads the contents of the .env file.
  • xargs takes the output of cat .env and converts it into a list of KEY=VALUE pairs that can be passed as arguments to export.

In short, this command parses each line of .env as if you were setting the variables manually in the shell.

Running export $(cat .env | xargs)

$ export $(cat .env | xargs)
$ echo $DATABASE_URL
your_database_url

Pros of using export $(cat .env | xargs)

  1. More Control Over Parsing: This command only exports valid KEY=VALUE pairs, filtering out lines that are empty or contain only comments, which can reduce errors.
  2. Works in Various Shells: Since xargs is POSIX-compliant, it may work more reliably across different environments and shells.

Cons of using export $(cat .env | xargs)

  1. Less Readability: For those unfamiliar with xargs, the command can seem more complex and harder to read.
  2. Possible Issues with Special Characters: This approach may not handle lines with spaces or special characters as gracefully as source would, potentially causing parsing errors.

Key differences and when to use each

  • Use source .env when:

    • Your .env file is simple and straightforward.
    • You’re working in an environment where running shell commands directly from a file won’t cause conflicts.
    • You need compatibility with files containing commands or conditional expressions.
  • Use export $(cat .env | xargs) when:

    • You need a more portable approach that works across various shells.
    • You prefer explicit exporting of variables without executing them directly.
    • You want more control over which lines from .env get exported as variables.

Final thoughts

Both methods have their place depending on your environment and specific needs. If you’re just loading basic environment variables into a shell session and have a well-formed .env file, source .env is often simpler and easier to read. For more controlled or portable setups, especially in scripts where shell consistency might vary, export $(cat .env | xargs) is a powerful alternative.

In most cases, choosing between these methods boils down to balancing simplicity with control. Understanding the differences will help you make an informed choice based on your project’s requirements.