DEV Community

Hank Wang
Hank Wang

Posted on

Batch to add / remove environment variables by Vercel CLI

Not sure why Vercel CLI 28.9.0 has no way to import whole .env file or all environment variables in one project or environment. Write a simple bash script to achieve it:

(Tested on macOS zsh)

Check current environment variables and backup

# List up all current environment variables
vercel env ls

# Backup
vercel env pull .env.production.bak --environment production
vercel env pull .env.development.bak --environment development
vercel env pull .env.preview.bak --environment preview
Enter fullscreen mode Exit fullscreen mode

Add environment variables

cat .env.production | sed 's/=/ /' | xargs -n 2 bash -c 'echo -n $1 | vercel env add $0 production'
cat .env.staging | sed 's/=/ /' | xargs -n 2 bash -c 'echo -n $1 | vercel env add $0 development'
cat .env.staging | sed 's/=/ /' | xargs -n 2 bash -c 'echo -n $1 | vercel env add $0 preview'
Enter fullscreen mode Exit fullscreen mode

Remove all Vercel environment variables

vercel env ls | sed 's/=/ /' | awk 'NR>2 {print $1, $3}' | xargs -n 2 bash -c 'vercel env rm $0 ${1,,} -y'
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
shubhamnagota profile image
Shubham Nagota

vercel env ls | sed 's/=/ /' | awk 'NR>2 {print $1}' | xargs -n 1 bash -c 'vercel env rm $0 ${1} development

vercel env ls | sed 's/=/ /' | awk 'NR>2 {print $1}' | xargs -n 1 bash -c 'vercel env rm $0 ${1} preview

vercel env ls | sed 's/=/ /' | awk 'NR>2 {print $1}' | xargs -n 1 bash -c 'vercel env rm $0 ${1} production

worked for me

Collapse
 
aakashgoplani profile image
Aakash Goplani

what is windows equivalent of the sam?