Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

linux source env file

# -o allexport enables all following variable definitions to be exported. +o allexport disables this feature.

set -o allexport
source .env
set +o allexport

or 

set -o allexport; source .env; set +o allexport
Comment

linux source env file

# The problem with source is that it requires the file to have a proper bash syntax, and some special characters will ruin it: =, ", ', <, >, and others. So in some cases you can just

source development.env

# But This version, however, withstands every special character in values:

set -a
source <(cat development.env | 
sed -e '/^#/d;/^s*$/d' -e "s/'/'''/g" -e "s/=(.*)/='1'/g")
set +a


  # Explanation:

  # -a means that every bash variable would become an environment variable
  # /^#/d removes comments (strings that start with #)
  # /^s*$/d removes empty strings, including whitespace
  # "s/'/'''/g" replaces every single quote with ''', which is a trick sequence in bash to produce a quote :)
  # "s/=(.*)/='1'/g" converts every a=b into a='b'
  # As a result, you are able to use special characters :)

To debug this code, replace source with cat and you'll see what this command produces.
Comment

linux source env file


set -a
. ./env.txt
set +a
Comment

linux source env file

# convenience command to prepend export to the beginning of
awk '{print "export " $0}' envfile 
Comment

linux source env file

# allows you to have empty lines for better readability
eval $(cat .env | sed 's/^/export /')

# Here is another sed solution, which does not run eval or require ruby:
source <(sed -E -n 's/[^#]+/export &/ p' ~/.env)

  # .env contents
  A=1
  #B=2

  # sample run
  $ sed -E -n 's/[^#]+/export &/ p' ~/.env
  export A=1
  #export B=2
Comment

linux source env file

# export.sh .env

set -a # export all variables created next
source $1
set +a # stop exporting
Comment

linux source env file

# To ignore lines that start with #, use this (thanks to Pete's comment):
export $(grep -v '^#' .env | xargs)

# And if you want to unset all of the variables defined in the file, use this:
unset $(grep -v '^#' .env | sed -E 's/(.*)=.*/1/' | xargs)
Comment

linux source env file


export $(xargs < .env)

# Explanation
# When we have a .env file like this:

key=val
foo=bar
Comment

PREVIOUS NEXT
Code Example
Shell :: shell get all lines that are in one file and not another 
Shell :: repo tool depth 
Shell :: centos open port pid 
Shell :: nano move line up 
Shell :: how to save a specific file in stash 
Shell :: Overwrite Line Batch 
Shell :: install obs screen recorder linux ubuntu mint 
Shell :: pyinstaller onefile hidden window 
Shell :: install stegsnow ubuntu 
Shell :: k8s taint 
Shell :: bash temp file extension 
Shell :: instalacion mkweb 
Shell :: airodump output to file 
Shell :: Error: No Homebrew ruby 2.6.8 available for arm64 processors! 
Shell :: how to upgrade cpu of my instance in aws 
Shell :: No project found at or above and neither was a --path specified 
Shell :: “Permission denied while trying to connect to the Docker daemon socket” while accessing docker image on jenkins 
Shell :: Setting File Permission Allow Webserver di linux 
Shell :: deploy command for ropsten network 
Shell :: git borrar rama local y remote 
Shell :: no matches found: *.dmg 
Shell :: debugging kubernete in intellij 
Shell :: Npm install instagram-private-api 
Shell :: crate db helm 
Shell :: kubectl delete pods wildcard 
Shell :: make a ZFS raid z volume 
Shell :: osx go version not updating 
Shell :: We can print the $SHELL environment variable to determine the current shell you are using. 
Shell :: why showing different +++++++++------ count git diff command 
Shell :: linux copy file 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =