Example command to export .env (dotenv) file definition as a shell environment variable (set -a; source .env; set + a;) #Linux #Shell

Command

set -a; source .env; set +a;

Example

echo "EXAMPLE1=ABC\nEXAMPLE2=DEF" >> .env

cat .env
EXAMPLE1=ABC
EXAMPLE2=DEF

env | grep EXAMPLE

set -a; source .env; set +a;

env | grep EXAMPLE
EXAMPLE1=ABC
EXAMPLE2=DEF

What is set -a

It seems that set -a can treat a variable definition as an environment variable definition, export. Run it "front" of the shell with the source command.

NAME
    set - Set or unset values of shell options and positional parameters.

SYNOPSIS
    set [--abefhkmnptuvxBCHP] [-o option-name] [arg ...]

DESCRIPTION
    Set or unset values of shell options and positional parameters.
    
    Change the value of shell attributes and positional parameters, or
    display the names and values of shell variables.
    
    Options:
      -a  Mark variables which are modified or created for export.

http://linuxcommand.org/lc3_man_pages/seth.html

Linux — what the source command is doing> it's not really meant to reload the environment-Qiita

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/3008

Recommended Posts

Example command to export .env (dotenv) file definition as a shell environment variable (set -a; source .env; set + a;) #Linux #Shell
export ENV by reading dotenv file by Shell command (set -a; source .env; set + a;) (#Shell #Linux)
How to output the output result of the Linux man command to a file
Zip-compress any file with the [shell] command to create a file and delete the original file.