export ENV by reading dotenv file by Shell command (set -a; source .env; set + a;) (#Shell #Linux)

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

Ref

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

Original by Github issue

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

Recommended Posts

export ENV by reading dotenv file by Shell command (set -a; source .env; set + a;) (#Shell #Linux)
Example command to export .env (dotenv) file definition as a shell environment variable (set -a; source .env; set + a;) #Linux #Shell
Linux [shell command]
Linux command [File operation]
Export a gzip-compressed text file
How to output the output result of the Linux man command to a file