Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

Simple YAML accessor for shell scripts

Project description


Simple scripts that allow read access to YAML files through command line.

This can be handy, if you want to get access to YAML data in your shell scripts.

This scripts supports only read access and it might not support all the subtilties of YAML specification. But it should support some handy basic query of YAML file.


This script could be used out of the box, take the shyaml file and use it directly.

If you want to use the distribution method I’ve chosen, which is distutils2, please first make sure you have distutils2, in which case you could do a:

pysetup install shyaml

It should be compatible with older distribution method, so this should also work:

pip install shyaml

Please note that if you got the code thanks to git. You’ll need to execute ./ in order to generate missing files as the CHANGELOG.rst and the Then you can choose to directly use the shyaml binary, or install it thanks to classical pysetup install (or python


shyaml takes its YAML input file from standard input ONLY. So there are some sample routine:

Let’s create a sample yaml file:

$ cat <<EOF > test.yaml
name: "MyName !!"
    how-much: 1.1
    how-many: 2
        - first
        - second
        - third
    maintainer: "Valentin Lab"
    description: |
        Multiline description:
        Line 1
        Line 2 1.2\more: 1.3\.more: 1.4

General browsing struct and displaying simple values

Simple query of simple attribute:

$ cat test.yaml | shyaml get-value name
MyName !!

Query nested attributes by using ‘.’ between key labels:

$ cat test.yaml | shyaml get-value

Get type of attributes:

$ cat test.yaml | shyaml get-type name
$ cat test.yaml | shyaml get-type

Parse structure

Get sub YAML from a structure attribute:

$ cat test.yaml | shyaml get-type subvalue
$ cat test.yaml | shyaml get-value subvalue
description: 'Multiline description:

  Line 1

  Line 2

how-many: 2
how-much: 1.1
maintainer: Valentin Lab
- first
- second
- third

Iteration through keys only:

$ cat test.yaml | shyaml keys

Iteration through keys only (0 terminated strings):

$ cat test.yaml | shyaml keys-0 subvalue | xargs -0 -n 1 echo "VALUE:"
VALUE: how-much
VALUE: things
VALUE: how-many
VALUE: maintainer
VALUE: description

Iteration though values only (0 terminated string highly recommended):

$ cat test.yaml | shyaml values-0 | \
  while read -r -d $'\0' value; do
      echo "RECEIVED: '$value'"
RECEIVED: '- first
- second
- third'
RECEIVED: 'Valentin Lab'
RECEIVED: 'Multiline description:
Line 1
Line 2'

Parse sequence

Query a sequence with get-value:

$ cat test.yaml | shyaml get-value subvalue.things
- first
- second
- third
$ cat test.yaml | shyaml get-value subvalue.things.0
$ cat test.yaml | shyaml get-value subvalue.things.-1
$ cat test.yaml | shyaml get-value subvalue.things.5
Error: list index error in path 'subvalue.things.5'.

More usefull, parse a list in one go with get-values:

$ cat test.yaml | shyaml get-values subvalue.things

Note that the action is called get-values, and that output is separated by \n chars, this can bring havoc if you are parsing values containing this character. Hopefully, shyaml has a get-values-0 to terminate strings by \0 char, which allows complete support of any type of values, including YAML. get-values outputs key and values for struct types and only values for sequence types:

$ cat test.yaml | shyaml get-values-0 subvalue | \
  while read -r -d $'\0' key value; do
      echo "'$key' -> '$value'"
'how-much' -> '1.1'
'things' -> '- first
- second
- third'
'how-many' -> '2'
'maintainer' -> 'Valentin Lab'
'description' -> 'Multiline description:
Line 1
Line 2'

You should also notice that values that are displayed are YAML compatible. So if they are complex, you can re-use shyaml on them to parse their content.

Keys containing ‘.’

Use and \\ to access keys with \ and \. to access keys with literal . in them. Just be mindful of shell escaping (example uses single quotes):

$ cat test.yaml | shyaml get-value 'subvalue\.how-much'
$ cat test.yaml | shyaml get-value 'subvalue\.how-much\\more'
$ cat test.yaml | shyaml get-value 'subvalue\.how-much\\.more' default

This last one didn’t escape correctly the last ., this is the correct version:

$ cat test.yaml | shyaml get-value 'subvalue\.how-much\\\.more' default

empty string keys

Yep, shyaml supports empty stringed keys. You might never have use for this one, but it’s in YAML specification. So shyaml supports it:

$ cat <<EOF > test.yaml
       a: foo
       "": bar
"": wiz

$ cat test.yaml | shyaml get-value empty-sub-key..
$ cat test.yaml | shyaml get-value ''

Usage string

A Quick remainder of what is available:

$ shyaml
    shyaml {get-value{,-0},get-type,keys{,-0},values{,-0}} KEY DEFAULT


0.2.1 (2013-11-23)


  • Python3 support. [Valentin Lab]


  • Keys can now be empty or contains dots . if they are properly escaped (fixes #5, thanks to Daniel Giribet) [Daniel Giribet]

0.2.0 (2013-05-03)


  • Support for iteration in sequence and struct in one go. [Valentin Lab]


  • Doc: forgot to mention ./ execution when getting the code from git, and be more clear about other means of installation. [Valentin Lab]

0.1.3 (2013-03-29)


  • Removed the spurious line feed at the end of any shyaml output. [Valentin Lab]

  • Support querying for… nothing. Which now returns the whole input YAML. [Valentin Lab]

    Before this fix, you couldn’t ask for shyaml get-value alone, even if it makes sense but is completely useless as it returns the whole YAML input.

0.1.2 (2013-03-23)


  • Support for list indexes (see README.rst). [Valentin Lab]
  • Catch exceptions when parsing structure and output a clean error message. [Valentin Lab]

0.1.1 (2013-02-27)


  • Doc: some minor enhancements, and an “Install” section. [Valentin Lab]

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for shyaml, version 0.2.1
Filename, size File type Python version Upload date Hashes
Filename, size shyaml-0.2.1.tar.gz (7.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page