Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 881 Bytes

File metadata and controls

25 lines (18 loc) · 881 Bytes

Get Multiple Values from a Bash Function

yq does a great job of parsing and extracting values from YAML files. However, it typically outputs the results on separate lines. If we can get them on the same line(xargs to the rescue), we can use "Here Document" or HereDoc to extract them into variables.

read -r url user password <<< "$(yq -r '.database | (.url,.user,.password)' < /conf/config.yml | xargs)"

Note: Later versions of yq may not have the -r shortcut, in which case you must use the longer form --unwrapScalar.

Here is a snippet of the YAML file.

---
# Database settings.
database:
  driverClass: org.h2.Driver
  user: sa
  password: sa
  url: jdbc:h2:./target/example

References

  1. Returning Multiple Values (“Tuples”-ish) from Bash Functions