Skip to content

Instantly share code, notes, and snippets.

@amjadjibon
Last active June 30, 2023 13:19
Show Gist options
  • Select an option

  • Save amjadjibon/43d5f248eb8384d1827738fcf3eec384 to your computer and use it in GitHub Desktop.

Select an option

Save amjadjibon/43d5f248eb8384d1827738fcf3eec384 to your computer and use it in GitHub Desktop.
SED Command: .env file hacks

SED Command: .env file hacks

Envfile: .env

# Start Environment

FOO=Bar

# This commant is to create confusion
# FOO=barred

HOST=localhost # 127.0.0.1

# This a comment

PORT=8080
# END Environment

Remove all comments

sed 's/ *#.*//g' .env

Output

FOO=Bar




HOST=localhost



PORT=8080

Remove all empty line

sed -e 's/ *#.*//g' -e '/^$/ d' .env

Output

FOO=Bar
HOST=localhost
PORT=8080

Turn into k8s env

sed -e 's/ *#.*//g' -e '/^$/ d' -e 's/^\(.*\)=\(.*\)$/- name: \1\n  value: \2/' .env

Output

- name: FOO
  value: Bar
- name: HOST
  value: localhost
- name: PORT
  value: 8080

Add "env:" at the begining of the file

echo 'env:'; sed -e 's/ *#.*//g' -e '/^$/ d' -e 's/^\(.*\)=\(.*\)$/- name: \1\n  value: \2/' .env

{ echo 'env:'; sed -e 's/ *#.*//g' -e '/^$/ d' -e 's/^\(.*\)=\(.*\)$/- name: \1\n  value: \2/' .env } > env.yaml

Output

env:
- name: FOO
  value: Bar
- name: HOST
  value: localhost
- name: PORT
  value: 8080

Export .env as Environment Variable

export $(sed -e 's/ *#.*//g' -e '/^$/ d' .env | xargs)

Turn into docker compose environment variables

sed -e 's/ *#.*//g' -e '/^$/ d' -e 's/^/- /' .env

Output

- FOO=Bar
- HOST=localhost
- PORT=8080

Add environment at begining

echo 'environment:'; sed -e 's/ *#.*//g' -e '/^$/ d' -e 's/^/- /' .env

Output

environment:
- FOO=Bar
- HOST=localhost
- PORT=8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment