The reliable way to read a YAML value from a bash script is to convert the document to JSON and query it with jq. Both of these work; pick one and be explicit about which yq you mean:
# Option A — Python + jq. Works anywhere Python 3 and jq exist.
yaml2json() { python3 -c 'import sys,yaml,json; json.dump(yaml.safe_load(sys.stdin), sys.stdout)'; }
NAME=$(yaml2json < config.yml | jq -r '.service.name')
# Option B — Mike Farah's Go yq (v4), installed deliberately, not from apt
NAME=$(yq -r '.service.name' config.yml)
Option A is real output from this machine: the pipeline printed api for .service.name, and .service.ports[] printed 8080 and 8443 on separate lines — which is what you want for a bash for loop. (The .yml extension used throughout these examples is interchangeable with .yaml — no parser treats them differently.)
Table of Contents
The apt-get install yq trap
This is the single biggest reason YAML-in-bash tutorials do not work when you follow them. sudo apt-get install yq on Ubuntu does not install the yq those tutorials are using.
There are two unrelated programs called yq:
- python-yq (kislyuk) — a thin wrapper that converts YAML to JSON and shells out to
jq. It depends on jq. Filter syntax is jq syntax. - Go yq (mikefarah) — a standalone Go binary with its own expression language and subcommands such as
e/eval. Latest releasev4.53.3, published 2026-06-06.
Queried against Ubuntu’s own archive API, the yq package situation is:
| Ubuntu release | yq package |
What it actually is | yq-go |
|---|---|---|---|
| 22.04 jammy | not in archive | — | not in archive |
| 24.04 noble | 3.1.0-3 (universe) | “Command-line YAML processor — jq wrapper for YAML documents” | not in archive |
| 26.04 resolute (current LTS) | 3.4.3-2 (universe) | same jq wrapper | not in archive |
| 26.10 stonking (development) | 3.4.3-3 | same jq wrapper | 4.53.3+ds-1 |
So on 22.04 the install command fails outright. On 24.04 and the current 26.04 LTS you get the jq wrapper, whose declared dependencies include jq, python3 and python3-yaml. Go yq is not packaged for any supported LTS; it first appears as yq-go in the 26.10 development series.
The consequence, run here with python-yq pinned to 3.4.3 — the exact version in Ubuntu 26.04 — against Go yq v4.53.3:
$ ./go-yq e '.a.b' t.yml
hello
exit=0
$ ./python-yq e '.a.b' t.yml
usage: yq [-h] [--yaml-output] [--yaml-roundtrip] ...
yq: error: argument files: can't open '.a.b':
[Errno 2] No such file or directory: '.a.b'
exit=2
python-yq has no e subcommand, so it reads e as the jq filter and .a.b as a filename; the correct form there is yq -r '.a.b' t.yml, which prints hello. If you want Go yq, install it from GitHub releases, Homebrew or snap — never assume apt install yq gave it to you. Check with yq --version: Go yq prints yq (https://github.com/mikefarah/yq/) version v4.x, python-yq prints yq 3.4.3 plus the jq version. The script that reproduces this page’s checks puts both programs in a scratch directory rather than installing them system-wide, and if mysql -u root connects it also leaves one scratch database behind and prints the command to drop it.
Ruby: YAML is already there
You do not need to install anything. YAML is in Ruby’s standard library, implemented by Psych:
$ ruby -e 'require "yaml"; puts "Psych #{Psych::VERSION}, Ruby #{RUBY_VERSION}"'
Psych 3.1.0, Ruby 2.6.10
A gem literally named yaml does exist on rubygems.org (0.4.0), but it is the gemified standard library, shipped with Ruby. gem install yaml is not a prerequisite for require "yaml" and telling readers otherwise sends them chasing a non-problem.
YAML.load changed meaning in Ruby 3.1
Psych 4, which ships with Ruby 3.1 and later, made YAML.load safe by default. It now rejects aliases, most non-primitive classes (including Date), and symbols — which is a large share of real configuration files. Code written against older tutorials breaks with Psych::BadAlias or Psych::DisallowedClass.
On Psych 4 / Ruby 3.1+, use one of:
YAML.unsafe_load(File.read("config.yml")) # old behaviour, only for trusted input
YAML.safe_load(File.read("config.yml"),
aliases: true, permitted_classes: [Date, Symbol]) # preferred
The Ruby available here is 2.6 with Psych 3.1, so only the pre-4 path could be tested. Under Psych 3.1, YAML.load returned a hash containing a merged alias and a Date; YAML.safe_load on the same file raised Psych::BadAlias: Unknown alias: d, and adding aliases: true, permitted_classes: [Date] made it succeed. That confirms the shape of the change — safe_load is what YAML.load became — but YAML.unsafe_load does not exist at all on Psych 3.1 (respond_to? returned false), so that call is Ruby 3.1+ only. The Psych 4 default itself is documentation-based here.
Python: don’t print the dict
A lot of examples end at print(yaml.safe_load(f)), which emits a Python dict repr — single quotes, True/None — that no shell tool can parse:
{'service': {'name': 'api', 'replicas': 3, 'ports': [8080, 8443], 'env': {'LOG_LEVEL': 'debug'}}}
Emit JSON instead and pipe to jq, as in Option A at the top. Also use python3 explicitly: on current Ubuntu there is no python command unless python-is-python3 is installed, and on the machine used here command -v python found nothing while python3 was 3.9.6 with PyYAML 6.0.2.
Never eval a YAML-derived string
The sed/awk parse_yaml() function that circulates on forums ends with eval $(parse_yaml config.yml). That is arbitrary code execution, not parsing. Given a file containing:
name: "$(touch /tmp/PWNED)"
the function emitted cfg_name="$(touch /tmp/PWNED)", and eval on that string created the file. That was reproduced on this machine. If a YAML file is ever written by CI, a template, a customer, or a dependency, eval hands it your shell. Use the JSON+jq route instead.
Load YAML into SQL with parameters, not string concatenation
Building SQL by concatenating YAML values into a string produces statements like this:
INSERT INTO employee (id, name) VALUES (1, "John Doe");
Two things are wrong. First, the double quotes: in standard SQL and in PostgreSQL they delimit identifiers, not string literals. MySQL accepts them as strings only because of its default sql_mode. Verified on MySQL 8.4.10 — the statement inserted fine with the stock sql_mode, but the identical statement under ANSI_QUOTES failed with:
ERROR 1054 (42S22): Unknown column 'Jane Roe' in 'field list'
Use single quotes for string literals, always. Second and more seriously, concatenating unescaped YAML values into SQL is textbook injection: a name field containing '); DROP TABLE employee;-- is executed, not stored. If you need to load YAML into a database, pass the values as parameters from a real client rather than assembling SQL in bash:
python3 - <<'PY'
import yaml, mysql.connector
rows = yaml.safe_load(open("employees.yml"))["employees"]
cnx = mysql.connector.connect(user="app", database="hr")
cur = cnx.cursor()
cur.executemany("INSERT INTO employee (id, name) VALUES (%s, %s)",
[(r["id"], r["name"]) for r in rows])
cnx.commit()
PY
What was verified here
Executed on macOS 26.6 (arm64) with Python 3.9.6/PyYAML 6.0.2, Ruby 2.6.10/Psych 3.1.0, jq 1.7.1, MySQL 8.4.10, python-yq 3.4.3 and Go yq v4.53.3: the yq syntax divergence, the Python-to-jq pipeline, the Ruby stdlib and safe_load behaviour, the eval code execution, and the MySQL double-quote behaviour under both sql_mode settings. Ubuntu package versions come from the Launchpad archive API and packages.ubuntu.com, not an Ubuntu machine. The Psych 4 default and the PostgreSQL identifier rule are documentation-based.