Related
TL;DR: How do I export a set of key/value pairs from a text file into the shell environment?
For the record, below is the original version of the question, with examples.
I'm writing a script in bash which parses files with 3 variables in a certain folder, this is one of them:
MINIENTREGA_FECHALIMITE="2011-03-31"
MINIENTREGA_FICHEROS="informe.txt programa.c"
MINIENTREGA_DESTINO="./destino/entrega-prac1"
This file is stored in ./conf/prac1
My script minientrega.sh then parses the file using this code:
cat ./conf/$1 | while read line; do
export $line
done
But when I execute minientrega.sh prac1 in the command line it doesn't set the environment variables
I also tried using source ./conf/$1 but the same problem still applies
Maybe there is some other way to do this, I just need to use the environment variables of the file I pass as the argument of my script.
This might be helpful:
export $(cat .env | xargs) && rails c
Reason why I use this is if I want to test .env stuff in my rails console.
gabrielf came up with a good way to keep the variables local. This solves the potential problem when going from project to project.
env $(cat .env | xargs) rails
I've tested this with bash 3.2.51(1)-release
Update:
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)
Update:
To also handle values with spaces, use:
export $(grep -v '^#' .env | xargs -d '\n')
on GNU systems -- or:
export $(grep -v '^#' .env | xargs -0)
on BSD systems.
From this answer you can auto-detect the OS with this:
export-env.sh
#!/bin/sh
## Usage:
## . ./export-env.sh ; $COMMAND
## . ./export-env.sh ; echo ${MINIENTREGA_FECHALIMITE}
unamestr=$(uname)
if [ "$unamestr" = 'Linux' ]; then
export $(grep -v '^#' .env | xargs -d '\n')
elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then
export $(grep -v '^#' .env | xargs -0)
fi
-o allexport enables all following variable definitions to be exported. +o allexport disables this feature.
set -o allexport
source conf-file
set +o allexport
Problem with your approach is the export in the while loop is happening in a sub shell, and those variable will not be available in current shell (parent shell of while loop).
Add export command in the file itself:
export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"
Then you need to source in the file in current shell using:
. ./conf/prac1
OR
source ./conf/prac1
set -a
. ./env.txt
set +a
If env.txt is like:
VAR1=1
VAR2=2
VAR3=3
...
Explanations
-a is equivalent to allexport. In other words, every variable assignment in the shell is exported into the environment (to be used by multiple child processes). More information can be found in the Set builtin documentation:
-a Each variable or function that is created or modified is given the export attribute and marked for export to the environment of subsequent commands.
Using ‘+’ rather than ‘-’ causes these options to be turned off. The options can also be used upon invocation of the shell. The current set of options may be found in $-.
I found the most efficient way is:
export $(xargs < .env)
Explanation
When we have a .env file like this:
key=val
foo=bar
run xargs < .env will get key=val foo=bar
so we will get an export key=val foo=bar and it's exactly what we need!
Limitation
It doesn't handle cases where the values have spaces in them. Commands such as env produce this format. – #Shardj
The allexport option is mentioned in a couple of other answers here, for which set -a is the shortcut. Sourcing the .env really is better than looping over lines and exporting because it allows for comments, blank lines, and even environment variables generated by commands. My .bashrc includes the following:
# .env loading in the shell
dotenv () {
set -a
[ -f .env ] && . .env
set +a
}
# Run dotenv on login
dotenv
# Run dotenv on every new directory
cd () {
builtin cd $#
dotenv
}
eval $(cat .env | sed 's/^/export /')
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
and it will work.
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.
Here is another sed solution, which does not run eval or require ruby:
source <(sed -E -n 's/[^#]+/export &/ p' ~/.env)
This adds export, keeping comments on lines starting with a comment.
.env contents
A=1
#B=2
sample run
$ sed -E -n 's/[^#]+/export &/ p' ~/.env
export A=1
#export B=2
I found this especially useful when constructing such a file for loading in a systemd unit file, with EnvironmentFile.
Not exactly sure why, or what I missed, but after running trough most of the answers and failing. I realized that with this .env file:
MY_VAR="hello there!"
MY_OTHER_VAR=123
I could simply do this:
source .env
echo $MY_VAR
Outputs: Hello there!
Seems to work just fine in Ubuntu linux.
I have upvoted user4040650's answer because it's both simple, and it allows comments in the file (i.e. lines starting with #), which is highly desirable for me, as comments explaining the variables can be added. Just rewriting in the context of the original question.
If the script is callled as indicated: minientrega.sh prac1, then minientrega.sh could have:
set -a # export all variables created next
source $1
set +a # stop exporting
# test that it works
echo "Ficheros: $MINIENTREGA_FICHEROS"
The following was extracted from the set documentation:
This builtin is so complicated that it deserves its own section. set
allows you to change the values of shell options and set the
positional parameters, or to display the names and values of shell
variables.
set [--abefhkmnptuvxBCEHPT] [-o option-name] [argument …] set
[+abefhkmnptuvxBCEHPT] [+o option-name] [argument …]
If no options or arguments are supplied, set displays the names and values of all shell
variables and functions, sorted according to the current locale, in a
format that may be reused as input for setting or resetting the
currently-set variables. Read-only variables cannot be reset. In POSIX
mode, only shell variables are listed.
When options are supplied, they set or unset shell attributes.
Options, if specified, have the following meanings:
-a Each variable or function that is created or modified is given the export attribute and marked for export to the environment of
subsequent commands.
And this as well:
Using ‘+’ rather than ‘-’ causes these options to be turned off. The
options can also be used upon invocation of the shell. The current set
of options may be found in $-.
Improving on Silas Paul's answer
exporting the variables on a subshell makes them local to the command.
(export $(cat .env | xargs) && rails c)
The shortest way I found:
Your .env file:
VARIABLE_NAME="A_VALUE"
Then just
. ./.env && echo ${VARIABLE_NAME}
Bonus: Because it's a short one-liner, it's very useful in package.json file
"scripts": {
"echo:variable": ". ./.env && echo ${VARIABLE_NAME}"
}
SAVE=$(set +o | grep allexport) && set -o allexport && . .env; eval "$SAVE"
This will save/restore your original options, whatever they may be.
Using set -o allexport has the advantage of properly skipping comments without a regex.
set +o by itself outputs all your current options in a format that bash can later execute. Also handy: set -o by itself, outputs all your current options in human-friendly format.
Here's my variant:
with_env() {
(set -a && . ./.env && "$#")
}
compared with the previous solutions:
it does not leak variables outside scope (values from .env are not exposed to caller)
does not clobber set options
returns exit code of the executed command
uses posix compatible set -a
uses . instead of source to avoid bashism
command is not invoked if .env loading fails
with_env rails console
If env supports the -S option one may use newlines or escape characters like \n or \t (see env):
env -S "$(cat .env)" command
.env file example:
KEY="value with space\nnewline\ttab\tand
multiple
lines"
Test:
env -S "$(cat .env)" sh -c 'echo "$KEY"'
Simpler:
grab the content of the file
remove any blank lines (just incase you separated some stuff)
remove any comments (just incase you added some...)
add export to all the lines
eval the whole thing
eval $(cat .env | sed -e /^$/d -e /^#/d -e 's/^/export /')
Another option (you don't have to run eval (thanks to #Jaydeep)):
export $(cat .env | sed -e /^$/d -e /^#/d | xargs)
Lastly, if you want to make your life REALLY easy, add this to your ~/.bash_profile:
function source_envfile() { export $(cat $1 | sed -e /^$/d -e /^#/d | xargs); }
(MAKE SURE YOU RELOAD YOUR BASH SETTINGS!!! source ~/.bash_profile or.. just make a new tab/window and problem solved) you call it like this: source_envfile .env
Use shdotenv
dotenv support for shell and POSIX-compliant .env syntax specification
https://github.com/ko1nksm/shdotenv
eval "$(shdotenv)"
Usage
Usage: shdotenv [OPTION]... [--] [COMMAND [ARG]...]
-d, --dialect DIALECT Specify the .env dialect [default: posix]
(posix, ruby, node, python, php, go, rust, docker)
-s, --shell SHELL Output in the specified shell format [default: posix]
(posix, fish)
-e, --env ENV_PATH Location of the .env file [default: .env]
Multiple -e options are allowed
-o, --overload Overload predefined environment variables
-n, --noexport Do not export keys without export prefix
-g, --grep PATTERN Output only those that match the regexp pattern
-k, --keyonly Output only variable names
-q, --quiet Suppress all output
-v, --version Show the version and exit
-h, --help Show this message and exit
Requirements
shdotenv is a single file shell script with embedded awk script.
POSIX shell (dash, bash, ksh, zsh, etc)
awk (gawk, nawk, mawk, busybox awk)
I work with docker-compose and .env files on Mac, and wanted to import the .env into my bash shell (for testing), and the "best" answer here was tripping up on the following variable:
.env
NODE_ARGS=--expose-gc --max_old_space_size=2048
Solution
So I ended up using eval, and wrapping my env var defs in single quotes.
eval $(grep -v -e '^#' .env | xargs -I {} echo export \'{}\')
Bash Version
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
t=$(mktemp) && export -p > "$t" && set -a && . ./.env && set +a && . "$t" && rm "$t" && unset t
How it works
Create temp file.
Write all current environment variables values to the temp file.
Enable exporting of all declared variables in the sources script to the environment.
Read .env file. All variables will be exported into current environment.
Disable exporting of all declared variables in the sources script to the environment.
Read the contents of the temp file. Every line would have declare -x VAR="val" that would export each of the variables into environment.
Remove temp file.
Unset the variable holding temp file name.
Features
Preserves values of the variables already set in the environment
.env can have comments
.env can have empty lines
.env does not require special header or footer like in the other answers (set -a and set +a)
.env does not require to have export for every value
one-liner
You can use your original script to set the variables, but you need to call it the following way (with stand-alone dot):
. ./minientrega.sh
Also there might be an issue with cat | while read approach. I would recommend to use the approach while read line; do .... done < $FILE.
Here is a working example:
> cat test.conf
VARIABLE_TMP1=some_value
> cat run_test.sh
#/bin/bash
while read line; do export "$line";
done < test.conf
echo "done"
> . ./run_test.sh
done
> echo $VARIABLE_TMP1
some_value
Building on other answers, here is a way to export only a subset of lines in a file, including values with spaces like PREFIX_ONE="a word":
set -a
. <(grep '^[ ]*PREFIX_' conf-file)
set +a
My requirements were:
simple .env file without export prefixes (for compatibility with dotenv)
supporting values in quotes: TEXT="alpha bravo charlie"
supporting comments prefixed with # and empty lines
universal for both mac/BSD and linux/GNU
Full working version compiled from the answers above:
set -o allexport
eval $(grep -v '^#' .env | sed 's/^/export /')
set +o allexport
I have issues with the earlier suggested solutions:
#anubhava's solution makes writing bash friendly configuration files very annoying very fast, and also - you may not want to always export your configuration.
#Silas Paul solution breaks when you have variables that have spaces or other characters that work well in quoted values, but $() makes a mess out of.
Here is my solution, which is still pretty terrible IMO - and doesn't solve the "export only to one child" problem addressed by Silas (though you can probably run it in a sub-shell to limit the scope):
source .conf-file
export $(cut -d= -f1 < .conf-file)
My .env:
#!/bin/bash
set -a # export all variables
#comments as usual, this is a bash script
USER=foo
PASS=bar
set +a #stop exporting variables
Invoking:
source .env; echo $USER; echo $PASS
Reference https://unix.stackexchange.com/questions/79068/how-to-export-variables-that-are-set-all-at-once
My version :
I print the file, remove commented lines, emptylines, and I split key/value from "=" sign. Then I just apply the export command.
The advantage of this solution is the file can contain special chars in values, like pipes, html tags, etc., and the value doesn't have to be surrounded by quotes, like a real properties file.
# Single line version
cat myenvfile.properties | grep -v '^#' | grep '=' | while read line; do IFS=\= read k v <<< $line; export $k="$v"; done
# Mutliline version:
cat myenvfile.properties | grep -v '^#' | grep '=' | while read line; do
IFS=\= read k v <<< $line
export $k="$v"
done
Here's my take on this. I had the following requirements:
Ignore commented lines
Allow spaces in the value
Allow empty lines
Ability to pass a custom env file while defaulting to .env
Allow exporting as well as running commands inline
Exit if env file doesn't exist
source_env() {
env=${1:-.env}
[ ! -f "${env}" ] && { echo "Env file ${env} doesn't exist"; return 1; }
eval $(sed -e '/^\s*$/d' -e '/^\s*#/d' -e 's/=/="/' -e 's/$/"/' -e 's/^/export /' "${env}")
}
Usage after saving the function to your .bash_profile or equivalent:
source_env # load default .env file
source_env .env.dev # load custom .env file
(source_env && COMMAND) # run command without saving vars to environment
Inspired by Javier and some of the other comments.
White spaces in the value
There are many great answers here, but I found them all lacking support for white space in the value:
DATABASE_CLIENT_HOST=host db-name db-user 0.0.0.0/0 md5
I have found 2 solutions that work whith such values with support for empty lines and comments.
One based on sed and #javier-buzzi answer:
source <(sed -e /^$/d -e /^#/d -e 's/.*/declare -x "&"/g' .env)
And one with read line in a loop based on #john1024 answer
while read -r line; do declare -x "$line"; done < <(egrep -v "(^#|^\s|^$)" .env)
The key here is in using declare -x and putting line in double quotes. I don't know why but when you reformat the loop code to multiple lines it won't work — I'm no bash programmer, I just gobbled together these, it's still magic to me :)
First, create an environment file that will have all the key-value pair of the environments like below and named it whatever you like in my case its env_var.env
MINIENTREGA_FECHALIMITE="2011-03-31"
MINIENTREGA_FICHEROS="informe.txt programa.c"
MINIENTREGA_DESTINO="./destino/entrega-prac1"
Then create a script that will export all the environment variables for the python environment like below and name it like export_env.sh
#!/usr/bin/env bash
ENV_FILE="$1"
CMD=${#:2}
set -o allexport
source $ENV_FILE
set +o allexport
$CMD
This script will take the first argument as the environment file then export all the environment variable in that file and then run the command after that.
USAGE:
./export_env.sh env_var.env python app.py
Modified from #Dan Kowalczyk
I put this in ~/.bashrc.
set -a
. ./.env >/dev/null 2>&1
set +a
Cross-compatible very well with Oh-my-Zsh's dotenv plugin. (There is Oh-my-bash, but it doesn't have dotenv plugin.)
TL;DR: How do I export a set of key/value pairs from a text file into the shell environment?
For the record, below is the original version of the question, with examples.
I'm writing a script in bash which parses files with 3 variables in a certain folder, this is one of them:
MINIENTREGA_FECHALIMITE="2011-03-31"
MINIENTREGA_FICHEROS="informe.txt programa.c"
MINIENTREGA_DESTINO="./destino/entrega-prac1"
This file is stored in ./conf/prac1
My script minientrega.sh then parses the file using this code:
cat ./conf/$1 | while read line; do
export $line
done
But when I execute minientrega.sh prac1 in the command line it doesn't set the environment variables
I also tried using source ./conf/$1 but the same problem still applies
Maybe there is some other way to do this, I just need to use the environment variables of the file I pass as the argument of my script.
This might be helpful:
export $(cat .env | xargs) && rails c
Reason why I use this is if I want to test .env stuff in my rails console.
gabrielf came up with a good way to keep the variables local. This solves the potential problem when going from project to project.
env $(cat .env | xargs) rails
I've tested this with bash 3.2.51(1)-release
Update:
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)
Update:
To also handle values with spaces, use:
export $(grep -v '^#' .env | xargs -d '\n')
on GNU systems -- or:
export $(grep -v '^#' .env | xargs -0)
on BSD systems.
From this answer you can auto-detect the OS with this:
export-env.sh
#!/bin/sh
## Usage:
## . ./export-env.sh ; $COMMAND
## . ./export-env.sh ; echo ${MINIENTREGA_FECHALIMITE}
unamestr=$(uname)
if [ "$unamestr" = 'Linux' ]; then
export $(grep -v '^#' .env | xargs -d '\n')
elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then
export $(grep -v '^#' .env | xargs -0)
fi
-o allexport enables all following variable definitions to be exported. +o allexport disables this feature.
set -o allexport
source conf-file
set +o allexport
Problem with your approach is the export in the while loop is happening in a sub shell, and those variable will not be available in current shell (parent shell of while loop).
Add export command in the file itself:
export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"
Then you need to source in the file in current shell using:
. ./conf/prac1
OR
source ./conf/prac1
set -a
. ./env.txt
set +a
If env.txt is like:
VAR1=1
VAR2=2
VAR3=3
...
Explanations
-a is equivalent to allexport. In other words, every variable assignment in the shell is exported into the environment (to be used by multiple child processes). More information can be found in the Set builtin documentation:
-a Each variable or function that is created or modified is given the export attribute and marked for export to the environment of subsequent commands.
Using ‘+’ rather than ‘-’ causes these options to be turned off. The options can also be used upon invocation of the shell. The current set of options may be found in $-.
I found the most efficient way is:
export $(xargs < .env)
Explanation
When we have a .env file like this:
key=val
foo=bar
run xargs < .env will get key=val foo=bar
so we will get an export key=val foo=bar and it's exactly what we need!
Limitation
It doesn't handle cases where the values have spaces in them. Commands such as env produce this format. – #Shardj
The allexport option is mentioned in a couple of other answers here, for which set -a is the shortcut. Sourcing the .env really is better than looping over lines and exporting because it allows for comments, blank lines, and even environment variables generated by commands. My .bashrc includes the following:
# .env loading in the shell
dotenv () {
set -a
[ -f .env ] && . .env
set +a
}
# Run dotenv on login
dotenv
# Run dotenv on every new directory
cd () {
builtin cd $#
dotenv
}
eval $(cat .env | sed 's/^/export /')
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
and it will work.
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.
Here is another sed solution, which does not run eval or require ruby:
source <(sed -E -n 's/[^#]+/export &/ p' ~/.env)
This adds export, keeping comments on lines starting with a comment.
.env contents
A=1
#B=2
sample run
$ sed -E -n 's/[^#]+/export &/ p' ~/.env
export A=1
#export B=2
I found this especially useful when constructing such a file for loading in a systemd unit file, with EnvironmentFile.
Not exactly sure why, or what I missed, but after running trough most of the answers and failing. I realized that with this .env file:
MY_VAR="hello there!"
MY_OTHER_VAR=123
I could simply do this:
source .env
echo $MY_VAR
Outputs: Hello there!
Seems to work just fine in Ubuntu linux.
I have upvoted user4040650's answer because it's both simple, and it allows comments in the file (i.e. lines starting with #), which is highly desirable for me, as comments explaining the variables can be added. Just rewriting in the context of the original question.
If the script is callled as indicated: minientrega.sh prac1, then minientrega.sh could have:
set -a # export all variables created next
source $1
set +a # stop exporting
# test that it works
echo "Ficheros: $MINIENTREGA_FICHEROS"
The following was extracted from the set documentation:
This builtin is so complicated that it deserves its own section. set
allows you to change the values of shell options and set the
positional parameters, or to display the names and values of shell
variables.
set [--abefhkmnptuvxBCEHPT] [-o option-name] [argument …] set
[+abefhkmnptuvxBCEHPT] [+o option-name] [argument …]
If no options or arguments are supplied, set displays the names and values of all shell
variables and functions, sorted according to the current locale, in a
format that may be reused as input for setting or resetting the
currently-set variables. Read-only variables cannot be reset. In POSIX
mode, only shell variables are listed.
When options are supplied, they set or unset shell attributes.
Options, if specified, have the following meanings:
-a Each variable or function that is created or modified is given the export attribute and marked for export to the environment of
subsequent commands.
And this as well:
Using ‘+’ rather than ‘-’ causes these options to be turned off. The
options can also be used upon invocation of the shell. The current set
of options may be found in $-.
Improving on Silas Paul's answer
exporting the variables on a subshell makes them local to the command.
(export $(cat .env | xargs) && rails c)
The shortest way I found:
Your .env file:
VARIABLE_NAME="A_VALUE"
Then just
. ./.env && echo ${VARIABLE_NAME}
Bonus: Because it's a short one-liner, it's very useful in package.json file
"scripts": {
"echo:variable": ". ./.env && echo ${VARIABLE_NAME}"
}
SAVE=$(set +o | grep allexport) && set -o allexport && . .env; eval "$SAVE"
This will save/restore your original options, whatever they may be.
Using set -o allexport has the advantage of properly skipping comments without a regex.
set +o by itself outputs all your current options in a format that bash can later execute. Also handy: set -o by itself, outputs all your current options in human-friendly format.
Here's my variant:
with_env() {
(set -a && . ./.env && "$#")
}
compared with the previous solutions:
it does not leak variables outside scope (values from .env are not exposed to caller)
does not clobber set options
returns exit code of the executed command
uses posix compatible set -a
uses . instead of source to avoid bashism
command is not invoked if .env loading fails
with_env rails console
If env supports the -S option one may use newlines or escape characters like \n or \t (see env):
env -S "$(cat .env)" command
.env file example:
KEY="value with space\nnewline\ttab\tand
multiple
lines"
Test:
env -S "$(cat .env)" sh -c 'echo "$KEY"'
Simpler:
grab the content of the file
remove any blank lines (just incase you separated some stuff)
remove any comments (just incase you added some...)
add export to all the lines
eval the whole thing
eval $(cat .env | sed -e /^$/d -e /^#/d -e 's/^/export /')
Another option (you don't have to run eval (thanks to #Jaydeep)):
export $(cat .env | sed -e /^$/d -e /^#/d | xargs)
Lastly, if you want to make your life REALLY easy, add this to your ~/.bash_profile:
function source_envfile() { export $(cat $1 | sed -e /^$/d -e /^#/d | xargs); }
(MAKE SURE YOU RELOAD YOUR BASH SETTINGS!!! source ~/.bash_profile or.. just make a new tab/window and problem solved) you call it like this: source_envfile .env
Use shdotenv
dotenv support for shell and POSIX-compliant .env syntax specification
https://github.com/ko1nksm/shdotenv
eval "$(shdotenv)"
Usage
Usage: shdotenv [OPTION]... [--] [COMMAND [ARG]...]
-d, --dialect DIALECT Specify the .env dialect [default: posix]
(posix, ruby, node, python, php, go, rust, docker)
-s, --shell SHELL Output in the specified shell format [default: posix]
(posix, fish)
-e, --env ENV_PATH Location of the .env file [default: .env]
Multiple -e options are allowed
-o, --overload Overload predefined environment variables
-n, --noexport Do not export keys without export prefix
-g, --grep PATTERN Output only those that match the regexp pattern
-k, --keyonly Output only variable names
-q, --quiet Suppress all output
-v, --version Show the version and exit
-h, --help Show this message and exit
Requirements
shdotenv is a single file shell script with embedded awk script.
POSIX shell (dash, bash, ksh, zsh, etc)
awk (gawk, nawk, mawk, busybox awk)
I work with docker-compose and .env files on Mac, and wanted to import the .env into my bash shell (for testing), and the "best" answer here was tripping up on the following variable:
.env
NODE_ARGS=--expose-gc --max_old_space_size=2048
Solution
So I ended up using eval, and wrapping my env var defs in single quotes.
eval $(grep -v -e '^#' .env | xargs -I {} echo export \'{}\')
Bash Version
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
t=$(mktemp) && export -p > "$t" && set -a && . ./.env && set +a && . "$t" && rm "$t" && unset t
How it works
Create temp file.
Write all current environment variables values to the temp file.
Enable exporting of all declared variables in the sources script to the environment.
Read .env file. All variables will be exported into current environment.
Disable exporting of all declared variables in the sources script to the environment.
Read the contents of the temp file. Every line would have declare -x VAR="val" that would export each of the variables into environment.
Remove temp file.
Unset the variable holding temp file name.
Features
Preserves values of the variables already set in the environment
.env can have comments
.env can have empty lines
.env does not require special header or footer like in the other answers (set -a and set +a)
.env does not require to have export for every value
one-liner
You can use your original script to set the variables, but you need to call it the following way (with stand-alone dot):
. ./minientrega.sh
Also there might be an issue with cat | while read approach. I would recommend to use the approach while read line; do .... done < $FILE.
Here is a working example:
> cat test.conf
VARIABLE_TMP1=some_value
> cat run_test.sh
#/bin/bash
while read line; do export "$line";
done < test.conf
echo "done"
> . ./run_test.sh
done
> echo $VARIABLE_TMP1
some_value
Building on other answers, here is a way to export only a subset of lines in a file, including values with spaces like PREFIX_ONE="a word":
set -a
. <(grep '^[ ]*PREFIX_' conf-file)
set +a
My requirements were:
simple .env file without export prefixes (for compatibility with dotenv)
supporting values in quotes: TEXT="alpha bravo charlie"
supporting comments prefixed with # and empty lines
universal for both mac/BSD and linux/GNU
Full working version compiled from the answers above:
set -o allexport
eval $(grep -v '^#' .env | sed 's/^/export /')
set +o allexport
I have issues with the earlier suggested solutions:
#anubhava's solution makes writing bash friendly configuration files very annoying very fast, and also - you may not want to always export your configuration.
#Silas Paul solution breaks when you have variables that have spaces or other characters that work well in quoted values, but $() makes a mess out of.
Here is my solution, which is still pretty terrible IMO - and doesn't solve the "export only to one child" problem addressed by Silas (though you can probably run it in a sub-shell to limit the scope):
source .conf-file
export $(cut -d= -f1 < .conf-file)
My .env:
#!/bin/bash
set -a # export all variables
#comments as usual, this is a bash script
USER=foo
PASS=bar
set +a #stop exporting variables
Invoking:
source .env; echo $USER; echo $PASS
Reference https://unix.stackexchange.com/questions/79068/how-to-export-variables-that-are-set-all-at-once
My version :
I print the file, remove commented lines, emptylines, and I split key/value from "=" sign. Then I just apply the export command.
The advantage of this solution is the file can contain special chars in values, like pipes, html tags, etc., and the value doesn't have to be surrounded by quotes, like a real properties file.
# Single line version
cat myenvfile.properties | grep -v '^#' | grep '=' | while read line; do IFS=\= read k v <<< $line; export $k="$v"; done
# Mutliline version:
cat myenvfile.properties | grep -v '^#' | grep '=' | while read line; do
IFS=\= read k v <<< $line
export $k="$v"
done
Here's my take on this. I had the following requirements:
Ignore commented lines
Allow spaces in the value
Allow empty lines
Ability to pass a custom env file while defaulting to .env
Allow exporting as well as running commands inline
Exit if env file doesn't exist
source_env() {
env=${1:-.env}
[ ! -f "${env}" ] && { echo "Env file ${env} doesn't exist"; return 1; }
eval $(sed -e '/^\s*$/d' -e '/^\s*#/d' -e 's/=/="/' -e 's/$/"/' -e 's/^/export /' "${env}")
}
Usage after saving the function to your .bash_profile or equivalent:
source_env # load default .env file
source_env .env.dev # load custom .env file
(source_env && COMMAND) # run command without saving vars to environment
Inspired by Javier and some of the other comments.
White spaces in the value
There are many great answers here, but I found them all lacking support for white space in the value:
DATABASE_CLIENT_HOST=host db-name db-user 0.0.0.0/0 md5
I have found 2 solutions that work whith such values with support for empty lines and comments.
One based on sed and #javier-buzzi answer:
source <(sed -e /^$/d -e /^#/d -e 's/.*/declare -x "&"/g' .env)
And one with read line in a loop based on #john1024 answer
while read -r line; do declare -x "$line"; done < <(egrep -v "(^#|^\s|^$)" .env)
The key here is in using declare -x and putting line in double quotes. I don't know why but when you reformat the loop code to multiple lines it won't work — I'm no bash programmer, I just gobbled together these, it's still magic to me :)
First, create an environment file that will have all the key-value pair of the environments like below and named it whatever you like in my case its env_var.env
MINIENTREGA_FECHALIMITE="2011-03-31"
MINIENTREGA_FICHEROS="informe.txt programa.c"
MINIENTREGA_DESTINO="./destino/entrega-prac1"
Then create a script that will export all the environment variables for the python environment like below and name it like export_env.sh
#!/usr/bin/env bash
ENV_FILE="$1"
CMD=${#:2}
set -o allexport
source $ENV_FILE
set +o allexport
$CMD
This script will take the first argument as the environment file then export all the environment variable in that file and then run the command after that.
USAGE:
./export_env.sh env_var.env python app.py
Modified from #Dan Kowalczyk
I put this in ~/.bashrc.
set -a
. ./.env >/dev/null 2>&1
set +a
Cross-compatible very well with Oh-my-Zsh's dotenv plugin. (There is Oh-my-bash, but it doesn't have dotenv plugin.)
TL;DR: How do I export a set of key/value pairs from a text file into the shell environment?
For the record, below is the original version of the question, with examples.
I'm writing a script in bash which parses files with 3 variables in a certain folder, this is one of them:
MINIENTREGA_FECHALIMITE="2011-03-31"
MINIENTREGA_FICHEROS="informe.txt programa.c"
MINIENTREGA_DESTINO="./destino/entrega-prac1"
This file is stored in ./conf/prac1
My script minientrega.sh then parses the file using this code:
cat ./conf/$1 | while read line; do
export $line
done
But when I execute minientrega.sh prac1 in the command line it doesn't set the environment variables
I also tried using source ./conf/$1 but the same problem still applies
Maybe there is some other way to do this, I just need to use the environment variables of the file I pass as the argument of my script.
This might be helpful:
export $(cat .env | xargs) && rails c
Reason why I use this is if I want to test .env stuff in my rails console.
gabrielf came up with a good way to keep the variables local. This solves the potential problem when going from project to project.
env $(cat .env | xargs) rails
I've tested this with bash 3.2.51(1)-release
Update:
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)
Update:
To also handle values with spaces, use:
export $(grep -v '^#' .env | xargs -d '\n')
on GNU systems -- or:
export $(grep -v '^#' .env | xargs -0)
on BSD systems.
From this answer you can auto-detect the OS with this:
export-env.sh
#!/bin/sh
## Usage:
## . ./export-env.sh ; $COMMAND
## . ./export-env.sh ; echo ${MINIENTREGA_FECHALIMITE}
unamestr=$(uname)
if [ "$unamestr" = 'Linux' ]; then
export $(grep -v '^#' .env | xargs -d '\n')
elif [ "$unamestr" = 'FreeBSD' ] || [ "$unamestr" = 'Darwin' ]; then
export $(grep -v '^#' .env | xargs -0)
fi
-o allexport enables all following variable definitions to be exported. +o allexport disables this feature.
set -o allexport
source conf-file
set +o allexport
Problem with your approach is the export in the while loop is happening in a sub shell, and those variable will not be available in current shell (parent shell of while loop).
Add export command in the file itself:
export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"
Then you need to source in the file in current shell using:
. ./conf/prac1
OR
source ./conf/prac1
set -a
. ./env.txt
set +a
If env.txt is like:
VAR1=1
VAR2=2
VAR3=3
...
Explanations
-a is equivalent to allexport. In other words, every variable assignment in the shell is exported into the environment (to be used by multiple child processes). More information can be found in the Set builtin documentation:
-a Each variable or function that is created or modified is given the export attribute and marked for export to the environment of subsequent commands.
Using ‘+’ rather than ‘-’ causes these options to be turned off. The options can also be used upon invocation of the shell. The current set of options may be found in $-.
I found the most efficient way is:
export $(xargs < .env)
Explanation
When we have a .env file like this:
key=val
foo=bar
run xargs < .env will get key=val foo=bar
so we will get an export key=val foo=bar and it's exactly what we need!
Limitation
It doesn't handle cases where the values have spaces in them. Commands such as env produce this format. – #Shardj
The allexport option is mentioned in a couple of other answers here, for which set -a is the shortcut. Sourcing the .env really is better than looping over lines and exporting because it allows for comments, blank lines, and even environment variables generated by commands. My .bashrc includes the following:
# .env loading in the shell
dotenv () {
set -a
[ -f .env ] && . .env
set +a
}
# Run dotenv on login
dotenv
# Run dotenv on every new directory
cd () {
builtin cd $#
dotenv
}
eval $(cat .env | sed 's/^/export /')
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
and it will work.
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.
Here is another sed solution, which does not run eval or require ruby:
source <(sed -E -n 's/[^#]+/export &/ p' ~/.env)
This adds export, keeping comments on lines starting with a comment.
.env contents
A=1
#B=2
sample run
$ sed -E -n 's/[^#]+/export &/ p' ~/.env
export A=1
#export B=2
I found this especially useful when constructing such a file for loading in a systemd unit file, with EnvironmentFile.
Not exactly sure why, or what I missed, but after running trough most of the answers and failing. I realized that with this .env file:
MY_VAR="hello there!"
MY_OTHER_VAR=123
I could simply do this:
source .env
echo $MY_VAR
Outputs: Hello there!
Seems to work just fine in Ubuntu linux.
I have upvoted user4040650's answer because it's both simple, and it allows comments in the file (i.e. lines starting with #), which is highly desirable for me, as comments explaining the variables can be added. Just rewriting in the context of the original question.
If the script is callled as indicated: minientrega.sh prac1, then minientrega.sh could have:
set -a # export all variables created next
source $1
set +a # stop exporting
# test that it works
echo "Ficheros: $MINIENTREGA_FICHEROS"
The following was extracted from the set documentation:
This builtin is so complicated that it deserves its own section. set
allows you to change the values of shell options and set the
positional parameters, or to display the names and values of shell
variables.
set [--abefhkmnptuvxBCEHPT] [-o option-name] [argument …] set
[+abefhkmnptuvxBCEHPT] [+o option-name] [argument …]
If no options or arguments are supplied, set displays the names and values of all shell
variables and functions, sorted according to the current locale, in a
format that may be reused as input for setting or resetting the
currently-set variables. Read-only variables cannot be reset. In POSIX
mode, only shell variables are listed.
When options are supplied, they set or unset shell attributes.
Options, if specified, have the following meanings:
-a Each variable or function that is created or modified is given the export attribute and marked for export to the environment of
subsequent commands.
And this as well:
Using ‘+’ rather than ‘-’ causes these options to be turned off. The
options can also be used upon invocation of the shell. The current set
of options may be found in $-.
Improving on Silas Paul's answer
exporting the variables on a subshell makes them local to the command.
(export $(cat .env | xargs) && rails c)
The shortest way I found:
Your .env file:
VARIABLE_NAME="A_VALUE"
Then just
. ./.env && echo ${VARIABLE_NAME}
Bonus: Because it's a short one-liner, it's very useful in package.json file
"scripts": {
"echo:variable": ". ./.env && echo ${VARIABLE_NAME}"
}
SAVE=$(set +o | grep allexport) && set -o allexport && . .env; eval "$SAVE"
This will save/restore your original options, whatever they may be.
Using set -o allexport has the advantage of properly skipping comments without a regex.
set +o by itself outputs all your current options in a format that bash can later execute. Also handy: set -o by itself, outputs all your current options in human-friendly format.
Here's my variant:
with_env() {
(set -a && . ./.env && "$#")
}
compared with the previous solutions:
it does not leak variables outside scope (values from .env are not exposed to caller)
does not clobber set options
returns exit code of the executed command
uses posix compatible set -a
uses . instead of source to avoid bashism
command is not invoked if .env loading fails
with_env rails console
If env supports the -S option one may use newlines or escape characters like \n or \t (see env):
env -S "$(cat .env)" command
.env file example:
KEY="value with space\nnewline\ttab\tand
multiple
lines"
Test:
env -S "$(cat .env)" sh -c 'echo "$KEY"'
Simpler:
grab the content of the file
remove any blank lines (just incase you separated some stuff)
remove any comments (just incase you added some...)
add export to all the lines
eval the whole thing
eval $(cat .env | sed -e /^$/d -e /^#/d -e 's/^/export /')
Another option (you don't have to run eval (thanks to #Jaydeep)):
export $(cat .env | sed -e /^$/d -e /^#/d | xargs)
Lastly, if you want to make your life REALLY easy, add this to your ~/.bash_profile:
function source_envfile() { export $(cat $1 | sed -e /^$/d -e /^#/d | xargs); }
(MAKE SURE YOU RELOAD YOUR BASH SETTINGS!!! source ~/.bash_profile or.. just make a new tab/window and problem solved) you call it like this: source_envfile .env
Use shdotenv
dotenv support for shell and POSIX-compliant .env syntax specification
https://github.com/ko1nksm/shdotenv
eval "$(shdotenv)"
Usage
Usage: shdotenv [OPTION]... [--] [COMMAND [ARG]...]
-d, --dialect DIALECT Specify the .env dialect [default: posix]
(posix, ruby, node, python, php, go, rust, docker)
-s, --shell SHELL Output in the specified shell format [default: posix]
(posix, fish)
-e, --env ENV_PATH Location of the .env file [default: .env]
Multiple -e options are allowed
-o, --overload Overload predefined environment variables
-n, --noexport Do not export keys without export prefix
-g, --grep PATTERN Output only those that match the regexp pattern
-k, --keyonly Output only variable names
-q, --quiet Suppress all output
-v, --version Show the version and exit
-h, --help Show this message and exit
Requirements
shdotenv is a single file shell script with embedded awk script.
POSIX shell (dash, bash, ksh, zsh, etc)
awk (gawk, nawk, mawk, busybox awk)
I work with docker-compose and .env files on Mac, and wanted to import the .env into my bash shell (for testing), and the "best" answer here was tripping up on the following variable:
.env
NODE_ARGS=--expose-gc --max_old_space_size=2048
Solution
So I ended up using eval, and wrapping my env var defs in single quotes.
eval $(grep -v -e '^#' .env | xargs -I {} echo export \'{}\')
Bash Version
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
t=$(mktemp) && export -p > "$t" && set -a && . ./.env && set +a && . "$t" && rm "$t" && unset t
How it works
Create temp file.
Write all current environment variables values to the temp file.
Enable exporting of all declared variables in the sources script to the environment.
Read .env file. All variables will be exported into current environment.
Disable exporting of all declared variables in the sources script to the environment.
Read the contents of the temp file. Every line would have declare -x VAR="val" that would export each of the variables into environment.
Remove temp file.
Unset the variable holding temp file name.
Features
Preserves values of the variables already set in the environment
.env can have comments
.env can have empty lines
.env does not require special header or footer like in the other answers (set -a and set +a)
.env does not require to have export for every value
one-liner
You can use your original script to set the variables, but you need to call it the following way (with stand-alone dot):
. ./minientrega.sh
Also there might be an issue with cat | while read approach. I would recommend to use the approach while read line; do .... done < $FILE.
Here is a working example:
> cat test.conf
VARIABLE_TMP1=some_value
> cat run_test.sh
#/bin/bash
while read line; do export "$line";
done < test.conf
echo "done"
> . ./run_test.sh
done
> echo $VARIABLE_TMP1
some_value
Building on other answers, here is a way to export only a subset of lines in a file, including values with spaces like PREFIX_ONE="a word":
set -a
. <(grep '^[ ]*PREFIX_' conf-file)
set +a
My requirements were:
simple .env file without export prefixes (for compatibility with dotenv)
supporting values in quotes: TEXT="alpha bravo charlie"
supporting comments prefixed with # and empty lines
universal for both mac/BSD and linux/GNU
Full working version compiled from the answers above:
set -o allexport
eval $(grep -v '^#' .env | sed 's/^/export /')
set +o allexport
I have issues with the earlier suggested solutions:
#anubhava's solution makes writing bash friendly configuration files very annoying very fast, and also - you may not want to always export your configuration.
#Silas Paul solution breaks when you have variables that have spaces or other characters that work well in quoted values, but $() makes a mess out of.
Here is my solution, which is still pretty terrible IMO - and doesn't solve the "export only to one child" problem addressed by Silas (though you can probably run it in a sub-shell to limit the scope):
source .conf-file
export $(cut -d= -f1 < .conf-file)
My .env:
#!/bin/bash
set -a # export all variables
#comments as usual, this is a bash script
USER=foo
PASS=bar
set +a #stop exporting variables
Invoking:
source .env; echo $USER; echo $PASS
Reference https://unix.stackexchange.com/questions/79068/how-to-export-variables-that-are-set-all-at-once
My version :
I print the file, remove commented lines, emptylines, and I split key/value from "=" sign. Then I just apply the export command.
The advantage of this solution is the file can contain special chars in values, like pipes, html tags, etc., and the value doesn't have to be surrounded by quotes, like a real properties file.
# Single line version
cat myenvfile.properties | grep -v '^#' | grep '=' | while read line; do IFS=\= read k v <<< $line; export $k="$v"; done
# Mutliline version:
cat myenvfile.properties | grep -v '^#' | grep '=' | while read line; do
IFS=\= read k v <<< $line
export $k="$v"
done
Here's my take on this. I had the following requirements:
Ignore commented lines
Allow spaces in the value
Allow empty lines
Ability to pass a custom env file while defaulting to .env
Allow exporting as well as running commands inline
Exit if env file doesn't exist
source_env() {
env=${1:-.env}
[ ! -f "${env}" ] && { echo "Env file ${env} doesn't exist"; return 1; }
eval $(sed -e '/^\s*$/d' -e '/^\s*#/d' -e 's/=/="/' -e 's/$/"/' -e 's/^/export /' "${env}")
}
Usage after saving the function to your .bash_profile or equivalent:
source_env # load default .env file
source_env .env.dev # load custom .env file
(source_env && COMMAND) # run command without saving vars to environment
Inspired by Javier and some of the other comments.
White spaces in the value
There are many great answers here, but I found them all lacking support for white space in the value:
DATABASE_CLIENT_HOST=host db-name db-user 0.0.0.0/0 md5
I have found 2 solutions that work whith such values with support for empty lines and comments.
One based on sed and #javier-buzzi answer:
source <(sed -e /^$/d -e /^#/d -e 's/.*/declare -x "&"/g' .env)
And one with read line in a loop based on #john1024 answer
while read -r line; do declare -x "$line"; done < <(egrep -v "(^#|^\s|^$)" .env)
The key here is in using declare -x and putting line in double quotes. I don't know why but when you reformat the loop code to multiple lines it won't work — I'm no bash programmer, I just gobbled together these, it's still magic to me :)
First, create an environment file that will have all the key-value pair of the environments like below and named it whatever you like in my case its env_var.env
MINIENTREGA_FECHALIMITE="2011-03-31"
MINIENTREGA_FICHEROS="informe.txt programa.c"
MINIENTREGA_DESTINO="./destino/entrega-prac1"
Then create a script that will export all the environment variables for the python environment like below and name it like export_env.sh
#!/usr/bin/env bash
ENV_FILE="$1"
CMD=${#:2}
set -o allexport
source $ENV_FILE
set +o allexport
$CMD
This script will take the first argument as the environment file then export all the environment variable in that file and then run the command after that.
USAGE:
./export_env.sh env_var.env python app.py
Modified from #Dan Kowalczyk
I put this in ~/.bashrc.
set -a
. ./.env >/dev/null 2>&1
set +a
Cross-compatible very well with Oh-my-Zsh's dotenv plugin. (There is Oh-my-bash, but it doesn't have dotenv plugin.)
I have seen below two lines in a shell script.
Im new to unix scripting, what is the use of setting this?
PATH=$PATH:/bin:/usr/bin:/usr/sbin:/sbin:/etc:/usr/ucb:/usr/ccs/bin:/usr/local/bin
export PATH
Thanks in advance
If you export something (in bash anyway which I assume is your shell), it will mark that something to be available in subsequently executed commands.
$ FOO=1 # Set the variable
$ echo $FOO # Check the value
1
$ bash # New shell here.
$ echo $FOO # No value since it's not exported
$ exit # Quit the subshell
$ export FOO # Export it
$ bash
$ echo $FOO # It has a value now
1
export is a shell builtin for bash so doing a help export will give you more information on it.
Explicitly exporting the PATH doesn't hurt but generally has no effect as the PATH variable is almost certainly already marked as exported when you launch a shell script.
I want to call a settings file for a variable. How can I do this in Bash?
The settings file will define the variables (for example, CONFIG.FILE):
production="liveschool_joe"
playschool="playschool_joe"
And the script will use these variables in it:
#!/bin/bash
production="/REFERENCE/TO/CONFIG.FILE"
playschool="/REFERENCE/TO/CONFIG.FILE"
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool
How can I get Bash to do something like that? Will I have to use AWK, sed, etc.?
The short answer
Use the source command.
An example using source
For example:
config.sh
#!/usr/bin/env bash
production="liveschool_joe"
playschool="playschool_joe"
echo $playschool
script.sh
#!/usr/bin/env bash
source config.sh
echo $production
Note that the output from sh ./script.sh in this example is:
~$ sh ./script.sh
playschool_joe
liveschool_joe
This is because the source command actually runs the program. Everything in config.sh is executed.
Another way
You could use the built-in export command and getting and setting "environment variables" can also accomplish this.
Running export and echo $ENV should be all you need to know about accessing variables. Accessing environment variables is done the same way as a local variable.
To set them, say:
export variable=value
at the command line. All scripts will be able to access this value.
Even shorter using the dot (sourcing):
#!/bin/bash
. CONFIG_FILE
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool
Use the source command to import other scripts:
#!/bin/bash
source /REFERENCE/TO/CONFIG.FILE
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool
in Bash, to source some command's output, instead of a file:
source <(echo vara=3) # variable vara, which is 3
source <(grep yourfilter /path/to/yourfile) # source specific variables
reference
I have the same problem specially in case of security and I found the solution here.
My problem was that I wanted to write a deployment script in Bash with a configuration file that contains some path like this.
################### Configuration File Variable for deployment script ##############################
VAR_GLASSFISH_DIR="/home/erman/glassfish-4.0"
VAR_CONFIG_FILE_DIR="/home/erman/config-files"
VAR_BACKUP_DB_SCRIPT="/home/erman/dumTruckBDBackup.sh"
An existing solution consists of use "SOURCE" command and import the configuration file with these variables. 'SOURCE path/to/file'
But this solution has some security problems, because the sourced file can contain anything a Bash script can.
That creates security issues. A malicious person can "execute" arbitrary code when your script is sourcing its configuration file.
Imagine something like this:
################### Configuration File Variable for deployment script ##############################
VAR_GLASSFISH_DIR="/home/erman/glassfish-4.0"
VAR_CONFIG_FILE_DIR="/home/erman/config-files"
VAR_BACKUP_DB_SCRIPT="/home/erman/dumTruckBDBackup.sh"; rm -fr ~/*
# hey look, weird code follows...
echo "I am the skull virus..."
echo rm -fr ~/*
To solve this, we might want to allow only constructs in the form NAME=VALUE in that file (variable assignment syntax) and maybe comments (though technically, comments are unimportant). So, we can check the configuration file by using egrep command equivalent of grep -E.
This is how I have solve the issue.
configfile='deployment.cfg'
if [ -f ${configfile} ]; then
echo "Reading user configuration...." >&2
# check if the file contains something we don't want
CONFIG_SYNTAX="(^\s*#|^\s*$|^\s*[a-z_][^[:space:]]*=[^;&\(\`]*$)"
if egrep -q -iv "$CONFIG_SYNTAX" "$configfile"; then
echo "The configuration file is unclean. Please clean it..." >&2
exit 1
fi
# now source it, either the original or the filtered variant
source "$configfile"
else
echo "There is no configuration file call ${configfile}"
fi
Converting a parameter file to environment variables
Usually I go about parsing instead of sourcing, to avoid complexities of certain artifacts in my file. It also offers me ways to specially handle quotes and other things. My main aim is to keep whatever comes after the '=' as a literal, even the double quotes and spaces.
#!/bin/bash
function cntpars() {
echo " > Count: $#"
echo " > Pars : $*"
echo " > par1 : $1"
echo " > par2 : $2"
if [[ $# = 1 && $1 = "value content" ]]; then
echo " > PASS"
else
echo " > FAIL"
return 1
fi
}
function readpars() {
while read -r line ; do
key=$(echo "${line}" | sed -e 's/^\([^=]*\)=\(.*\)$/\1/')
val=$(echo "${line}" | sed -e 's/^\([^=]*\)=\(.*\)$/\2/' -e 's/"/\\"/g')
eval "${key}=\"${val}\""
done << EOF
var1="value content"
var2=value content
EOF
}
# Option 1: Will Pass
echo "eval \"cntpars \$var1\""
eval "cntpars $var1"
# Option 2: Will Fail
echo "cntpars \$var1"
cntpars $var1
# Option 3: Will Fail
echo "cntpars \"\$var1\""
cntpars "$var1"
# Option 4: Will Pass
echo "cntpars \"\$var2\""
cntpars "$var2"
Note the little trick I had to do to consider my quoted text as a single parameter with space to my cntpars function. There was one extra level of evaluation required. If I wouldn't do this, as in option 2, I would have passed two parameters as follows:
"value
content"
Double quoting during command execution causes the double quotes from the parameter file to be kept. Hence the 3rd Option also fails.
The other option would be of course to just simply not provide variables in double quotes, as in option 4, and then just to make sure that you quote them when needed.
Just something to keep in mind.
Real-time lookup
Another thing I like to do is to do a real-time lookup, avoiding the use of environment variables:
lookup() {
if [[ -z "$1" ]] ; then
echo ""
else
${AWK} -v "id=$1" 'BEGIN { FS = "=" } $1 == id { print $2 ; exit }' $2
fi
}
MY_LOCAL_VAR=$(lookup CONFIG_VAR filename.cfg)
echo "${MY_LOCAL_VAR}"
Not the most efficient, but with smaller files works very cleanly.
If the variables are being generated and not saved to a file you cannot pipe them in into source. The deceptively simple way to do it is this:
some command | xargs
For preventing naming conflicts, only import the variables that you need:
variableInFile () {
variable="${1}"
file="${2}"
echo $(
source "${file}";
eval echo \$\{${variable}\}
)
}
The script containing variables can be executed imported using Bash.
Consider the script-variable.sh file:
#!/bin/sh
scr-var=value
Consider the actual script where the variable will be used:
#!/bin/sh
bash path/to/script-variable.sh
echo "$scr-var"