YAML to Bash Converter — Free Online

YAML to Bash Exports Converter

Paste YAML and get one export NAME='value' line per setting, ready to source from a shell script, a Dockerfile RUN step or a CI job. Nested keys are joined with _ into uppercase names. Conversion runs in your browser; nothing is uploaded.

How this tool handles it

The YAML is parsed once, then the same flattening converter used by the JSON to Bash page writes the file, so the output for the same data matches that page. The YAML page has no options, so the output always uses export, single quotes and _ as the separator.

  • Each leaf becomes one assignment. Nested keys are joined with _ and uppercased, so app.port becomes APP_PORT.
  • List items get an index suffix. database.replicas: [db2, db3] becomes DATABASE_REPLICAS_0 and DATABASE_REPLICAS_1. A list is never written as a bash array.
  • Values are single-quoted, so $, backticks and spaces are never expanded. A single quote inside a value is written as '\''.
  • Names are sanitized. Each run of characters other than letters and digits becomes one _, so my-app becomes MY_APP. A name that starts with a digit gets a leading _.
  • null and '' both become ''. An empty mapping or list is kept as the text '{}' or '[]', so nothing is dropped.
  • Booleans and dates are text. true becomes 'true', and 2024-01-15 becomes '2024-01-15'.
  • Numbers are written as JavaScript prints them. 0x1F becomes '31', and an integer longer than 2^53 loses its last digits.
  • Multi-line values keep real line breaks inside the single quotes.
  • Collisions are reported. When two keys map to one name, a # warning: line says so, and both assignments are written, so the last one wins.
  • Some input is refused. A top-level list gives Root must be an object, and a file with more than one YAML document is refused.

Example

Input:

app:
  name: demo
  port: 8080
database:
  replicas: [db2, db3]

Output:

export APP_NAME='demo'
export APP_PORT='8080'
export DATABASE_REPLICAS_0='db2'
export DATABASE_REPLICAS_1='db3'

Using the output

Save the output as config.sh, then source it:

source ./config.sh
echo "$APP_NAME is on port $APP_PORT"

This prints demo is on port 8080. Variables set with export are inherited by every program the script starts, which is how most tools read their configuration.

Gotchas

Hex and big integers are rewritten as decimal

port: 0x1F
id: 12345678901234567890
export PORT='31'
export ID='12345678901234567000'

Quote them in the YAML to keep the text: port: "0x1F" produces export PORT='0x1F'.

A single quote is escaped, not removed

password: "it's"
export PASSWORD='it'\''s'

The shell reads 'it'\''s' as it's. Do not edit the escape by hand; the value is correct as written.

Multi-line values span several lines

script: |
  echo one
  echo two
export SCRIPT='echo one
echo two
'

The shell keeps the line breaks, which is what a script value needs. A line-based tool such as grep or a .env loader will see the second line as a separate line, so use the YAML to .env output for those.

Two keys can produce one variable

a-b: 1
a_b: 2
# warning: A_B is produced by a-b and a_b; the last one wins
export A_B='1'
export A_B='2'

Both lines are written. Sourcing the file leaves A_B set to 2.

Lists are not bash arrays

hosts: [a, b]
export HOSTS_0='a'
export HOSTS_1='b'

The output has no declare -a line, so ${HOSTS[@]} is empty. Read the numbered variables, or write the array in your script.

FAQ

Is my YAML uploaded to a server?

No. The conversion runs in your browser, and the converter makes no network request with your input.

Why does every line start with export?

The output is written for source, and export makes each variable visible to the programs the script starts. Use the YAML to .env page for a file without export.

Can the top level of my YAML be a list?

No. The keys of the root become variable names, so the root has to be a mapping. A top-level list gives Root must be an object. Put the list under a key, such as items: [...].

What happens if the file contains two YAML documents?

The converter refuses it with This YAML has 2 documents; convert one document at a time. Convert each document on its own.