1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00
Files
til/unix/load-env-vars-in-bash-script.md
2023-06-15 10:47:10 -05:00

1.2 KiB

Load Env Vars In Bash Script

I have a file with environment variables in my current directory named something like .env.local. I want to load those variabls into my environment in the context of a bash script. That can be accomplished by exporting them at the beginning of the script with a line like this:

export $(egrep -v '^#' .env.local | xargs)

This uses egrep with the -v '^#' inverted match pattern to excluded any comment lines. Then xargs is going to remove any excess whitespace and echo the sequence env var entries. All of which will be the argument passed to export which adds them to the environment.

Here is an example of using it in a script that uses one of those secret env var values as a bearer token in a cURL request.

#!/bin/bash

# Load environment variables from .env.local file
export $(egrep -v '^#' .env.local | xargs)

# Now you can use the environment variable in your CURL command
curl -L \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $GITHUB_ACCESS_TOKEN"\
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/jbranchaud/github-actions-experiment/actions/workflows/playwright.yml/dispatches \
  -d '{"ref":"main"}'