Managing environment variables in multiple projects can be a hassle, especially when you need to switch between different .env
files. To make this process easier, I’ve created a simple Bash script that helps you sync .env
files between your projects and a centralized folder. This blog will guide you through setting up and using the script.
Create the Script File:
Open your terminal and create a new file named env_sync.sh
.
nano env_sync.sh
Copy and paste the following script into the file:
#!/bin/bash
# Define paths
ENV_FOLDER="$HOME/envs"
# Function to sync .env file from ~/envs based on current directory name
sync_envs() {
local action="$1"
# Get the current directory name (project name)
local current_dir=$(basename "$(pwd)")
local pwd=$(pwd)
case "$action" in
"copy")
# Copy .env file to ~/envs/projectname/
if [ ! -d "$ENV_FOLDER/$current_dir" ]; then
mkdir -p "$ENV_FOLDER/$current_dir"
fi
echo "$current_dir"
cp "$pwd/".env* "$ENV_FOLDER/$current_dir/"
echo ".env* files copied to $ENV_FOLDER/$current_dir/"
# Git operations
if [ ! -d "$ENV_FOLDER/$current_dir/.git" ]; then
git -C "$ENV_FOLDER/$current_dir" init
fi
git -C "$ENV_FOLDER/$current_dir" add .env*
git -C "$ENV_FOLDER/$current_dir" commit -m "Copied .env* files from $pwd to $ENV_FOLDER/$current_dir/ at $(date)"
;;
"move")
# Move .env file from ~/envs/projectname/ to current directory
if ls "$ENV_FOLDER/$current_dir/".env* 1> /dev/null 2>&1; then
mv "$ENV_FOLDER/$current_dir/".env* ./
echo ".env* files moved from $ENV_FOLDER/$current_dir/ to current directory."
# Git operations
if [ ! -d "$ENV_FOLDER/$current_dir/.git" ]; then
git -C "$ENV_FOLDER/$current_dir" init
fi
git -C "$ENV_FOLDER/$current_dir" add .env*
git -C "$ENV_FOLDER/$current_dir" commit -m "Moved .env* files from $ENV_FOLDER/$current_dir/ to $pwd at $(date)"
else
echo "Error: .env* file not found in $ENV_FOLDER/$current_dir/"
exit 1
fi
;;
*)
echo "Usage: env_sync [copy|move]"
exit 1
;;
esac
}
# Function to copy .env file from ~/envs based on manually provided directory name
copy_env_by_name() {
local dir_name="$1"
# Check if .env file exists in ~/envs/directory_name/
if ls "$ENV_FOLDER/$dir_name/".env* 1> /dev/null 2>&1; then
cp "$ENV_FOLDER/$dir_name/".env* ./
echo ".env* files copied from $ENV_FOLDER/$dir_name/ to current directory."
# Git operations
if [ ! -d "$ENV_FOLDER/$dir_name/.git" ]; then
git -C "$ENV_FOLDER/$dir_name" init
fi
git -C "$ENV_FOLDER/$dir_name" add .env*
git -C "$ENV_FOLDER/$dir_name" commit -m "Copied .env* files from $ENV_FOLDER/$dir_name/ to current directory at $(date)"
else
echo "Error: .env* files not found in $ENV_FOLDER/$dir_name/"
fi
}
# Function to list directories in ~/envs
list_envs_directories() {
echo "Directories in $ENV_FOLDER:"
ls -d $ENV_FOLDER/*/ | xargs -n 1 basename
}
# Help function
show_help() {
echo "Usage: $0 [option]"
echo "Options:"
echo " copy Copy .env* from current directory to ~/envs/projectname/"
echo " move Move .env* from ~/envs/projectname/ to current directory"
echo " ls List directories in $ENV_FOLDER"
echo " get <dir> Copy .env* from ~/envs/<dir> to current directory"
echo " dir <pattern> List directories in $ENV_FOLDER matching <pattern>"
echo " --help Show this help message"
}
# Main script logic
case "$1" in
"copy")
sync_envs copy
;;
"move")
sync_envs move
;;
"ls")
list_envs_directories
;;
"get")
if [ -n "$2" ]; then
copy_env_by_name "$2"
else
echo "Error: Please provide a directory name. Usage: env_sync get directory_name"
exit 1
fi
;;
"dir")
if [ -n "$2" ]; then
list_envs_directories | grep "$2"
else
echo "Error: Please provide a directory name to search. Usage: env_sync dir directory_name"
exit 1
fi
;;
"--help")
show_help
;;
*)
echo "Error: Invalid option. Use '--help' for usage."
exit 1
;;
esac
Make the Script Executable:
chmod +x env_sync.sh
To make the script easily accessible from anywhere, you can set up an alias in your shell configuration file (~/.bashrc
for Bash or ~/.zshrc
for Zsh).
Open Your Shell Configuration File:
nano ~/.bashrc
nano ~/.zshrc
Add the Alias:
envm
:
alias envm="$HOME/path_to_script/env_sync.sh"
path_to_script
with the actual path to where you saved env_sync.sh
.Apply the Changes:
source ~/.bashrc
source ~/.zshrc
With the alias set up, you can use the script with the envm
command:
Copy .env Files to Central Folder:
envm copy
Move .env Files from Central Folder to Project Directory:
envm move
List All Project Directories:
envm ls
Copy .env Files by Directory Name:
envm get <project_name>
Search for Directories by Pattern:
envm dir <pattern>
Show Help:
envm --help
The script automatically initializes a Git repository in the central folder if it doesn’t exist and commits changes each time you copy or move .env
files. This makes it easy to track changes and revert to previous versions if needed.
.env
FilesIf you add a remote repository and push your changes to GitHub or another Git hosting service, your environment variables could be exposed publicly. To avoid this, make sure to keep the envs
folder local and do not push it to a remote repository.
Initialize a Project:
myproject
.cd ~/projects/myproject
Copy .env Files to Central Folder:
.env
files to the central folder:
envm copy
List Project Directories:
envm ls
Copy .env Files from a Specific Project:
.env
files from a specific project folder:
envm get myproject
This script helps streamline the management of environment variables across different projects by providing a simple way to sync .env
files. With a few commands, you can copy, move, and list .env
files effortlessly. Feel free to customize the script further to suit your workflow needs. By setting up an alias, you can easily use the script from any directory, making your development process more efficient and