Declaring, Exporting, and Reading Dynamic Variables in Bash

If you want to dynamically define and export variable names in Bash here is the TLDR;

# Define the name of the variable

# Declare it and export it
declare -gx "$key"="some-value"

To then access that value via a dynamically generated variable name

# Create a variable that contains the variable name

# Read the value

Read the man page for declare for more details and read this article for a really good explanation and further examples.

To read the declare man page

help declare

Leave a Reply