33 lines
853 B
Markdown
33 lines
853 B
Markdown
---
|
|
title: Get .env file variables into bash scripts
|
|
tags:
|
|
- bash
|
|
categories: []
|
|
author: Max
|
|
date: 2023-01-01 04:07:00
|
|
---
|
|
|
|
If your server has a .env file, it may be necessary to parse those variables into a bash script. This can be accomplished by using the 'source' command.
|
|
|
|
A .env file is an important part of any application set up because it allows you to separate credentials of the app into different environments and pulls them out of the git repository.
|
|
|
|
Contents of a .env file may look like this:
|
|
DB_USERNAME=root
|
|
DB_PASSWORD=S0S3CUR3!
|
|
|
|
## How to pull .env variables into a bash script:
|
|
|
|
Make sure to start with the right [shebang][1] at the top of your file.
|
|
|
|
> #!/bin/bash
|
|
|
|
Pull in .env variables
|
|
|
|
> source ~/.env
|
|
|
|
Then reference those variables in your script
|
|
|
|
> echo "$DB_USERNAME"
|
|
|
|
[1]: https://earthly.dev/blog/understanding-bash/
|