Skip to content

Using fx in Shell Scripts and CI Pipelines ​

Read JSON values into bash variables, loop over arrays, branch on JSON content, and build JSON payloads from shell values with fx. Install fx first. Every example here works both in an interactive shell and in CI.

Read a JSON Value Into a Bash Variable ​

Wrap fx in command substitution. Top-level strings print raw, without quotes, so the variable holds the plain value:

bash
name=$(fx .name < config.json)
echo "$name"

Input:

json
{
  "name": "billing-api",
  "version": "2.4.1",
  "port": 8080,
  "debug": false,
  "tags": ["payments", "internal"],
  "database": {"host": "db.internal", "port": 5432}
}

Output:

billing-api

Numbers and booleans also print as bare text (8080, false). Objects and arrays print as pretty JSON:

bash
fx .database < config.json
json
{
  "host": "db.internal",
  "port": 5432
}

INFO

fx config.json .name, fx .name < config.json, and cat config.json | fx .name all work in scripts. In fx 39.2.0 and earlier, a file argument was ignored when stdin was not a terminal (CI jobs, while read loops); if you are on an older version, update fx or use < config.json.

Handle Missing Keys and Default Values ​

A missing key prints undefined to stderr, nothing to stdout, and exits with code 0, so $(...) gives an empty string. Use ?? to supply a default:

bash
fx '.timeout ?? 30' < config.json
30

Accessing a property of a missing object throws and exits with code 1. Use optional chaining:

bash
fx 'x.cache?.ttl ?? "none"' < config.json
none

Loop Over a JSON Array in Bash ​

list prints each array element on its own line, ready for while read:

bash
fx '?.enabled' '@.name' list < services.json | while read -r svc; do
  echo "deploying $svc"
done

Input:

json
[
  {"name": "auth", "url": "https://auth.example.com/health", "enabled": true},
  {"name": "billing", "url": "https://billing.example.com/health", "enabled": true},
  {"name": "search", "url": "https://search.example.com/health", "enabled": false},
  {"name": "notify", "url": "https://notify.example.com/health", "enabled": true}
]

Output of the fx part:

auth
billing
notify

Read Multiple Fields per Array Item ​

Emit one line per item with space-separated fields and let read split them:

bash
fx '?.enabled' '@`${x.name} ${x.url}`' list < services.json | while read -r name url; do
  curl -fsS "$url" > /dev/null || echo "$name is down"
done

Output of the fx part:

auth https://auth.example.com/health
billing https://billing.example.com/health
notify https://notify.example.com/health

list pretty-prints objects across several lines. To loop over whole objects, print each as compact JSON:

bash
fx '?.enabled' 'map(s => JSON.stringify(s))' list < services.json
{"name":"auth","url":"https://auth.example.com/health","enabled":true}
{"name":"billing","url":"https://billing.example.com/health","enabled":true}
{"name":"notify","url":"https://notify.example.com/health","enabled":true}

Pass Bash Variables Into fx ​

fx has no process.env (it prints ReferenceError: process is not defined) and no --arg flag. The safe approach is to send the shell value as data, not code. fx -r 'JSON.stringify(x)' turns a raw string into a JSON string. Put it in front of the file and slurp both values into one array:

bash
svc=billing
printf '%s\n' "$svc" | fx -r 'JSON.stringify(x)' | cat - services.json \
  | fx -s '([name, services]) => services.find(s => s.name === name)'

Output:

json
{
  "name": "billing",
  "url": "https://billing.example.com/health",
  "enabled": true
}

For trusted values, interpolating into a double-quoted expression is shorter. Escape the inner quotes:

bash
fx "x.find(s => s.name === \"$svc\").url" < services.json
https://billing.example.com/health

A value containing " or \ breaks this form or injects code, so use the first form for user input.

Use JSON Values in a Bash if Statement ​

Compare the printed value:

bash
if [ "$(fx .debug < config.json)" = "true" ]; then
  echo "debug is enabled"
fi

To fail a CI step, throw from the expression. fx prints the error to stderr and exits with code 1:

bash
fx 'x => { if (x.port !== 443) throw new Error("port must be 443") }' < config.json
  x => { if (x.port !== 443) throw new Error("port must be 443") }
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Error: port must be 443

Invalid JSON also exits with code 1, so fx . < file.json > /dev/null works as a syntax check. fx accepts comments and trailing commas by default; add --strict to reject them: fx --strict . < config.json > /dev/null || exit 1.

Build JSON From Shell Variables ​

-rs reads raw lines into an array of strings. Destructure it into an object. fx handles all quoting:

bash
printf '%s\n' "$name" "$version" | fx -rs '([name, version]) => ({name, version})'

Output:

json
{
  "name": "billing-api",
  "version": "2.4.1"
}

Convert KEY=value lines into an object. The same works on filtered env output (env | grep '^APP_' | fx ...):

bash
printf 'APP_ENV=prod\nAPP_PORT=8080\n' | fx -rs 'x => Object.fromEntries(x.map(l => l.split("=")))'

Output:

json
{
  "APP_ENV": "prod",
  "APP_PORT": "8080"
}

Use fx in GitHub Actions ​

Install the binary with the install script (it uses sudo, which GitHub-hosted runners allow), then pass values between steps through $GITHUB_OUTPUT:

yaml
steps:
  - uses: actions/checkout@v4

  - name: Install fx
    run: curl https://fx.wtf/install.sh | sh

  - name: Read version
    id: meta
    run: echo "version=$(fx .version < package.json)" >> "$GITHUB_OUTPUT"

  - name: Deploy
    run: ./deploy.sh "${{ steps.meta.outputs.version }}"

For other install methods, including the npx fx variant, see install.

See Also ​