DEV Community

Shoichi Okaniwa
Shoichi Okaniwa

Posted on • Originally published at qiita.com

Copy Firebase Environment Variables to Bash

Introduction

Firebase functions allow you to set environment variables. For an easy-to-understand guide on setting them, please refer to this article: How to Set Environment Variables in Firebase.

I wanted to use these environment variables in Bash as well, so I devised a command to copy them.

Example of Environment Variables

Assume the following environment variables are registered in Firebase functions:

{
  "hoge": {
    "client_id": "fuga"
    "client_secret": "piyo",
  }
}
Enter fullscreen mode Exit fullscreen mode

The goal here is to copy the client_id and client_secret into Bash.

Installing jq

To parse JSON, we need the command-line tool jq.

# Mac
brew install jq

# Windows
choco install jq

# Ubuntu
apt-get install jq

# CentOS
yum install jq
Enter fullscreen mode Exit fullscreen mode

Registering in Bash Environment Variables

You can register them with the following command:

export $(firebase functions:config:get | jq -r '.hoge' | jq -r 'keys[] as $k | "export \($k)=\(.[$k])"')
Enter fullscreen mode Exit fullscreen mode

Replace hoge with the appropriate value as needed.

This method was inspired by the article Export JSON key:value to environment variables.

Verifying Bash Environment Variables

$ echo ${client_id}
fuga

$ echo ${client_secret}
piyo
Enter fullscreen mode Exit fullscreen mode

The variables were successfully copied.

Conclusion

The following articles were invaluable references. Thank you for the clear guidance.

Top comments (0)