Answers for "linux source env file"

1

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.
Posted by: Guest on October-09-2021
1

linux source env file

set -a
. ./env.txt
set +a
Posted by: Guest on October-09-2021
1

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
Posted by: Guest on October-09-2021
1

linux source env file

# export.sh .env

set -a # export all variables created next
source $1
set +a # stop exporting
Posted by: Guest on October-09-2021
0

linux source env file

export $(xargs < .env)

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

key=val
foo=bar
Posted by: Guest on October-09-2021
1

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
Posted by: Guest on October-09-2021
0

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)
Posted by: Guest on October-09-2021
1

linux source env file

# convenience command to prepend export to the beginning of
awk '{print "export " $0}' envfile
Posted by: Guest on October-09-2021

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language