env-replacer
is a command-line tool designed to streamline the process of replacing environment variables in configuration files. It reads variables from a .env
file and substitutes placeholders within specified files or directories, ensuring smooth and safe configuration management.
- Efficiently replaces
${VAR_NAME}
placeholders with corresponding values from.env
. - Supports recursive replacement for all files within a directory and its subdirectories.
- Ensures safe replacement: placeholders for variables not present in the
.env
file remain unchanged. - Option to output to a specified file, leaving the original file unchanged.
- Simple and lightweight, facilitating easy integration into deployment and configuration workflows.
- Bash (Bourne Again SHell) available on Unix-like systems.
-
Clone the repository:
git clone https://github.com/yourusername/env-replacer.git cd env-replacer
-
Make sure
replace_env.sh
has execute permissions:chmod +x replace_env.sh
Assume your .env
file contains the following variables:
# .env
DB_PASSWORD=mysecretpassword
API_URL=https://api.example.com
To replace variables in a single file (configs/app.conf
):
./replace_env.sh .env configs/app.conf
Contents of configs/app.conf
before:
# Example config file
db_password=${DB_PASSWORD}
api_url=${API_URL}
After running replace_env.sh
:
# Example config file
db_password=mysecretpassword
api_url=https://api.example.com
Assume the following directory structure (configs/
):
configs/
├── app.conf
└── nested/
├── config1.conf
└── config2.conf
To replace variables recursively in the configs/
directory:
./replace_env.sh .env configs
After running replace_env.sh
, contents of configs/app.conf
, configs/nested/config1.conf
, and configs/nested/config2.conf
will be updated accordingly.
To specify an output file (configs/app.conf
to configs/app_updated.conf
):
./replace_env.sh .env configs/app.conf configs/app_updated.conf
Contents of configs/app.conf
before running replace_env.sh
:
# Example config file
db_password=${DB_PASSWORD}
api_url=${API_URL}
After running replace_env.sh
:
# Example config file
db_password=mysecretpassword
api_url=https://api.example.com
Contents of configs/app_updated.conf
after running replace_env.sh
will be the same as configs/app.conf
, while configs/app.conf
remains unchanged.
Assume the following configuration file (configs/app_safe.conf
):
# Example config file
db_password=${DB_PASSWORD}
api_url=${API_URL}
other_var=${OTHER_VAR}
To replace variables in the file while leaving undefined placeholders unchanged:
./replace_env.sh .env configs/app_safe.conf
Contents of configs/app_safe.conf
before:
# Example config file
db_password=${DB_PASSWORD}
api_url=${API_URL}
other_var=${OTHER_VAR}
After running replace_env.sh
:
# Example config file
db_password=mysecretpassword
api_url=https://api.example.com
other_var=${OTHER_VAR}
...
To ensure the script works correctly, you can run the included test script. The test script verifies single file replacement, directory recursive replacement, specifying output files, and safe replacement features.
-
Make sure
test_replace_env.sh
has execute permissions:chmod +x test_replace_env.sh
-
Run the test script:
./test_replace_env.sh
The test script will output the results of each test and indicate whether they passed or failed.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please fork the repository and submit a pull request with your improvements.