Mastering YAML Processing in Command Line

Nowadays, YAML is used for configuring almost everything (for better or worse), so whether you're DevOps engineer working with Kubernetes or Ansible, or developer configuring logging in Python or CI/CD with GitHub Actions - you have to deal with YAML files at least from time-to-time. Therefore, being able to efficiently query and manipulate YAML is essential skill for all of us - engineers. The best way to learn that is by mastering YAML processing tool such as yq , which can make you way more efficient at many of your daily tasks, from simple lookups to complex manipulations. So, let's go through and learn all that yq has to offer - including traversing, selecting, sorting, reducing and much more!

Before we begin using yq , we first need to install it. When you google yq though, you will find two projects/repositories. First of them, at https://github.com/kislyuk/yq is wrapper around jq - the JSON processor. If you're already familiar with jq you might want to grab this one and use the syntax you already know. In this article though, we will use the other - a bit more popular project - from https://github.com/mikefarah/yq . This version does not 100% match the jq syntax, but its advantage is that it's dependency free (does not depend on jq ), for more context on the differences, see following GitHub issue ).

To install it, head over to docs and choose installation method suitable for your system, just make sure you install version 4, as that's what we will work with here. On top of that, you might want to setup shell completion, information about that is available at https://mikefarah.gitbook.io/yq/commands/shell-completion .

Now that we have it installed, we also need some YAML file or document to test the commands we will be running. For that we will use the following files, which have all the common things you could find in YAML - attributes (plain and nested), arrays and various value types (string, integers and booleans):

With this out of the way, let's learn the basics!

All the commands we will be running will start with the same base of yq eval , followed by quoted expression and your YAML file. One exception to this would be pretty-printing of YAML files, in which case you would omit the expression - for example yq eval some.yaml - in case you're familiar with jq , then this is equivalent to cat some.json | jq . .

Optionally we can also tack on some extra flags, some of the more useful ones would be -C to force colored output, -I n to set indentation of output to n spaces, or -P for pretty-printing.

As for the basic expressions, there's a lot of things we can do, but the most common one is traversing YAMLs, or in other words - looking up some key in the YAML document. This is done using . (dot) operator and in the most basic form, this would look like so:

In addition to the basic map navigation, you will often want to lookup specific index in an array (using [N] ):

And finally, you might also find splat operator useful which flattens maps/arrays (notice the difference with the first example we looked at):

Apart from basic traversing you might also want to get familiar with selecting , which allows you to filter by boolean expressions. For this we use select(. == "some-pattern-here") . Here, simple example can be filtering based on leading digits:

This example also shows usage of pipe ( | ) - we use it to first navigate to the part of the document which we want to filter and then pass it on to select(...) .

In the above example we used == to find fields that are equal to the pattern, but you can also use != to match ones that are not equal. Additionally you can omit the select function altogether and instead of values you will get only boolean results of the matching:

Whether you're completely new to yq or you've been using it for a while, you'll surely run into issues where you will have no idea why your query doesn't return what you want. In those situations you can use -v flag to produce verbose output, which might give you info as to why the query behaves the way it does.

Advanced Querying

The previously section showed the basics, which are often sufficient for quick lookups and filtering, but sometimes you might want to use more advanced functions and operators, for example when automating certain tasks that involves YAML input and/or output. So, let's explore a few more things that yq has to offer.

Sometimes it might be useful to sort keys in the document, for example if you're versioning your YAMLs in git or just for general readability. It's also very convenient if you need to diff 2 YAML files. To do this we can use 'sortKeys(...)' function:

If the input YAML document is dynamic and you're not sure what keys will be present, it might make sense to first check for their presence with has("key") :

Similar to the case with has("key") , you might need to first get the dynamic list of keys before making certain operations with the document, for that you can use keys function:

Checking length of a value might be necessary for input filtering/validation or to make sure the value doesn't overflow some predefined bounds. This is done using length function:

For automation tasks that have parametrized inputs, you will surely need to pass environment variables into yq queries. You can obviously use normal shell environment variables, but you will end up with very tricky and hard to read quote escaping. Therefore, it might be better to use yq 's env() function instead:

To simplify processing of some fields or arrays you can also use some string function such as join or split to concatenate or breakup text:

Last and probably most complex example for this section is transformation of data using ireduce . To have a good reason to use this function you would need a quite complex YAML document which is not something I want to dump here. So, instead, to at least give you an idea of how the function works, let's use it to implement "poor man's" version of join from previous example:

This one isn't as self explanatory as the previous ones, so let's break it down a bit. First half ( .user.orders[] as $item ireduce ) of the query takes some iterable field (sequence) from YAML and assigns it to variable - in this case $item . In the second part, we define initial value ""; (empty string) and an expression that will be evacuated for each $item - here that would be the value that was there previously, joined with space ( (. + " ") ) followed by the item we are currently iterating over ( + $item ).

Manipulating and Modifying

Most of the time you will need to only do searches, lookups and filtering of existing documents, but from time to time you might need to also manipulate YAMLs and create new ones from them. yq provides a couple of operators to do these kinds of tasks, so let's go over them briefly and see a few examples.

The simplest one is union operator, which is really just a , (comma). It allows us to combine results of multiple queries. This can be useful if you need to extract multiple parts of YAML at the same time, but cannot do it with single query:

Another fairly common use-case would be adding record to array or concatenating 2 arrays. This is done with + (plus) operator:

Another handy one is update operator ( = ), which ( surprise, surprise ) updates some field. Very simple example of updating log level in our sample YAML:

It's important to point out here, that by default the result is sent to standard output and not to the original file. To make in-place update, you will need to use the -i option.

There a few more operators available, but aren't particularly useful (most of the time), so I won't show you bunch of examples that probably would not help you, instead I will give you some links to docs in case you want to know dig a little deeper:

  • Number subtraction
  • Multiplication (merging)

Handy Examples

Now that we know the theory, let's look at some examples and handy commands that you can incorporate into your workflow right away.

For obvious reasons, we start with Kubernetes as it's probably the most popular project that uses YAML for configuration. The simplest, yet very useful thing that yq can help us with is pretty-printing Kubernetes resources or querying specific sections of a manifest:

Another thing we can do is list resource name and specific attribute. This can be handy for finding or extracting all listening ports of Services or for example to lookup pods image for every pod in namespace:

Notice that above we had to use .items[] because when you get all instances of a resource, the returned Kind is a List of items .

In case of resources such as Pods, Deployments or Services which often have many instances in each namespace, it might be undesirable to just dump all of them into console and sift through them manually. So, instead you could filter them on some attribute, for example list name and listening port of only the services that are exposed on particular port:

As all Kubernetes "YAML Engineers" know, sometimes it can be difficult to remember all the fields in some particular resource, so why not just query all the keys for example for Deployment's spec.template.spec ?

Moving on from Kubernetes, how about some docker-compose ? Maybe you need to temporarily delete some section such as volumes or healthcheck - well here you go (this is destructive, so be careful):

In similar manner, you could also remove task from Ansible Playbook. Speaking of which - how about changing remote_user in all tasks in an Ansible Playbook - here, let's change it to root :

Closing Thoughts

I hope this "crash course" will help you get started with using yq , but as with any tool, you will only learn to use it by practicing and actually doing real world tasks, so next time you need to lookup something in YAML file, don't just dump it into terminal, but rather write a yq query to do the work for you. Also, if you're struggling to come up with query for your particular task and Google search doesn't turn up anything useful, try searching for solution that uses jq instead - the query syntax is almost the same and you might have better luck searching for jq solutions, considering that it's more popular/commonly used tool.

DEV Community

DEV Community

vik-codes

Posted on Oct 3, 2020 • Updated on Oct 14, 2020

yq : A command line tool that will help you handle your YAML resources better

When you want to update the fields in your YAML files, the standard practice is to use a templating tool that can replace values dynamically. However, Kubernetes does not provide any templating mechanism of its own as the deployed manifests are supposed to be static YAML files.

yq is a great command-line tool for templating YAML files, and it’s a lighter weight option to popular tools such as Helm and Kustomize .

What is yq ?

yq is a command-line tool designed to transform YAML. It is similar to jq which focuses on transforming JSON instead of YAML.

What can it do?

yq can take a YAML file as an input and:

  • Read values from the file.
  • Add new values.
  • Update Existing values.
  • Generate new YAML files.
  • Convert YAML into JSON.
  • Merge two or more YAML files.

Installation

You can install yq on Mac OS with:

On Linux with:

In case you don't have the add-apt-repository command installed, you can install it with apt-get install software- properties-common .

If you're on Windows, you can download the executable from Github .

Use case scenarios

1. reading yaml values.

Say you are working with the following Pod:

You could read the value for the environment variable ENV with the following command:

The command works as follows:

  • yq r is the command to read a value from the YAML file.
  • pod.yaml is the file path of the YAML that you want to read.
  • spec.containers[0].env[0].value is the query path.

2. Changing YAML values

Let’s take the previous example here. Say you wish to deploy the app in a production environment and change the URL to the production database.

Here’s how you can do the same using just a single command:

If you notice here, yq printed the result on the standard output. If you prefer to edit the YAML in place, you should add the -i flag.

The advantage of using yq over say sed (with bash) is that unlike sed , yq understands the YAML format, and can navigate and mangle the structured markup.

Note: sed treats files as strings and it doesn't mind if the file isn't a valid YAML.

3. Merging YAML files

Let's assume that you want to inject an extra container to all the Pods submitted to the cluster.

But instead of using an Admission Webhook , you decide to add an extra command in your deployment script.

You could save the YAML configuration for the extra container as a YAML file:

Assuming you have a Pod like this:

You can execute the following command and merge the two YAMLs:

Please notice the --append flag that is necessary to append values to an array. You can find more details in the official documentation .

The output should have a proxy named Envoy as an additional container:

Please note that yq sorts the YAML fields in the output alphabetically, so the order of fields in your output could be different from the > above listing.

In other words, the two YAML files are merged into one.

Limitations of using yq ?

While yq works great with transforming YAML, it does have a few issues of its own:

The two YAML files are merged at the top level. For instance, you cannot add a chunk of YAML file under .spec.containers[] .

The order of the files matters. If you invert the order, yq keeps envoy-pod for the Pod's name in metadata.name .

You have to tell yq explicitly when to append and overwrite values . Since those are flags that apply to the whole document, it's hard to get the granularity right.

Even with its limitations, yq has a lot of potential for use, especially if you are working on smaller projects.

Alternative(s)

If you wish to apply more complex transformations – Kustomize is a better option than yq . Kustomize uses a YAML file called kustomization.yaml to decide how to template the YAML – thus making all the changes traceable.

You can learn more about Kustomize and other interesting alternatives here: Templating YAML with real code

Top comments (7)

pic

Templates let you quickly answer FAQs or store snippets for re-use.

moseskarunia profile image

  • Location Bandung, Indonesia
  • Education Bachelor of IT
  • Work Freelancer & Software House Co-Owner
  • Joined Jul 5, 2019

The new v4 one is really different from v3. Check this out. mikefarah.gitbook.io/yq/upgrading-...

vikcodes profile image

  • Joined Oct 2, 2020

Will check it out, thanks!

bilalinamdar profile image

  • Joined Jul 22, 2020

I think this is outdated but was usefull. Kindly create v4 guide so it will be relavent. Because the above guide doesn't work with newer release that is v4. Good article this helped alot.

Thanks for the feedback Bilal! Will look into it.

slashpai profile image

  • Education M.Tech Computer and Information Science
  • Work Senior Software Engineer
  • Joined Mar 18, 2020

Thanks for sharing this!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .

Hide child comments as well

For further actions, you may consider blocking this person and/or reporting abuse

karanm645 profile image

Crafting my Code Chronicles Blog

Karan Mehta - Feb 16

snehalkadwe profile image

Understanding $this, self, parent, and static keywords in PHP and Laravel

Snehal Rajeev Moon - Feb 16

shiviyer profile image

How to optmize PostgreSQL query performance tuning effective_cache_size ?

Shiv - Feb 16

devteles profile image

"reactStrictMode" no Next.js

Rafael Teles Vital - Feb 16

Once suspended, vikcodes will not be able to comment or publish posts until their suspension is removed.

Once unsuspended, vikcodes will be able to comment and publish posts again.

Once unpublished, all posts by vikcodes will become hidden and only accessible to themselves.

If vikcodes is not suspended, they can still re-publish their posts from their dashboard.

Once unpublished, this post will become invisible to the public and only accessible to vik-codes.

They can still re-publish the post if they are not suspended.

Thanks for keeping DEV Community safe. Here is what you can do to flag vikcodes:

vikcodes consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy.

Unflagging vikcodes will restore default visibility to their posts.

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Search code, repositories, users, issues, pull requests...

Provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I cannot use bash variables anymore. #606

@DeLoWaN

DeLoWaN commented Dec 21, 2020

@DeLoWaN

mikefarah commented Dec 21, 2020

  • 👍 11 reactions
  • ❤️ 2 reactions
  • 🚀 2 reactions

Sorry, something went wrong.

@mikefarah

mikefarah commented Jan 10, 2021

  • 👍 1 reaction

@Maraudingas

Maraudingas commented Nov 15, 2021

@bencat-sixense

bencat-sixense commented Dec 7, 2021

@aerlaut

No branches or pull requests

@mikefarah

How-To Geek

How to work with variables in bash.

Want to take your Linux command-line skills to the next level? Here's everything you need to know to start working with variables.

Hannah Stryker / How-To Geek

Quick Links

Variables 101, examples of bash variables, how to use bash variables in scripts, how to use command line parameters in scripts, working with special variables, environment variables, how to export variables, how to quote variables, echo is your friend, key takeaways.

  • Variables are named symbols representing strings or numeric values. They are treated as their value when used in commands and expressions.
  • Variable names should be descriptive and cannot start with a number or contain spaces. They can start with an underscore and can have alphanumeric characters.
  • Variables can be used to store and reference values. The value of a variable can be changed, and it can be referenced by using the dollar sign $ before the variable name.

Variables are vital if you want to write scripts and understand what that code you're about to cut and paste from the web will do to your Linux computer. We'll get you started!

Variables are named symbols that represent either a string or numeric value. When you use them in commands and expressions, they are treated as if you had typed the value they hold instead of the name of the variable.

To create a variable, you just provide a name and value for it. Your variable names should be descriptive and remind you of the value they hold. A variable name cannot start with a number, nor can it contain spaces. It can, however, start with an underscore. Apart from that, you can use any mix of upper- and lowercase alphanumeric characters.

Here, we'll create five variables. The format is to type the name, the equals sign = , and the value. Note there isn't a space before or after the equals sign. Giving a variable a value is often referred to as assigning a value to the variable.

We'll create four string variables and one numeric variable,

my_name=Dave

my_boost=Linux

his_boost=Spinach

this_year=2019

Defining variables in Linux.

To see the value held in a variable, use the echo command. You must precede the variable name with a dollar sign $ whenever you reference the value it contains, as shown below:

echo $my_name

echo $my_boost

echo $this_year

Using echo to display the values held in variables in a terminal window

Let's use all of our variables at once:

echo "$my_boost is to $me as $his_boost is to $him (c) $this_year"

echo

The values of the variables replace their names. You can also change the values of variables. To assign a new value to the variable, my_boost , you just repeat what you did when you assigned its first value, like so:

my_boost=Tequila

my_boost=Tequila in a terminal window

If you re-run the previous command, you now get a different result:

echo

So, you can use the same command that references the same variables and get different results if you change the values held in the variables.

We'll talk about quoting variables later. For now, here are some things to remember:

  • A variable in single quotes ' is treated as a literal string, and not as a variable.
  • Variables in quotation marks " are treated as variables.
  • To get the value held in a variable, you have to provide the dollar sign $ .
  • A variable without the dollar sign $ only provides the name of the variable.

Correct an incorrect examples of referencing variables in a terminal window

You can also create a variable that takes its value from an existing variable or number of variables. The following command defines a new variable called drink_of_the_Year, and assigns it the combined values of the my_boost and this_year variables:

drink_of-the_Year="$my_boost $this_year"

echo drink_of_the-Year

drink_of-the_Year=

Scripts would be completely hamstrung without variables. Variables provide the flexibility that makes a script a general, rather than a specific, solution. To illustrate the difference, here's a script that counts the files in the /dev directory.

Type this into a text file, and then save it as fcnt.sh (for "file count"):

#!/bin/bashfolder_to_count=/devfile_count=$(ls $folder_to_count | wc -l)echo $file_count files in $folder_to_count

Before you can run the script, you have to make it executable, as shown below:

chmod +x fcnt.sh

chmod +x fcnt.sh in a terminal window

Type the following to run the script:

./fcnt.sh in a terminal window

This prints the number of files in the /dev directory. Here's how it works:

  • A variable called folder_to_count is defined, and it's set to hold the string "/dev."
  • Another variable, called file_count , is defined. This variable takes its value from a command substitution. This is the command phrase between the parentheses $( ) . Note there's a dollar sign $ before the first parenthesis. This construct $( ) evaluates the commands within the parentheses, and then returns their final value. In this example, that value is assigned to the file_count variable. As far as the file_count variable is concerned, it's passed a value to hold; it isn't concerned with how the value was obtained.
  • The command evaluated in the command substitution performs an ls file listing on the directory in the folder_to_count variable, which has been set to "/dev." So, the script executes the command "ls /dev."
  • The output from this command is piped into the wc command. The -l (line count) option causes wc to count the number of lines in the output from the ls command. As each file is listed on a separate line, this is the count of files and subdirectories in the "/dev" directory. This value is assigned to the file_count variable.
  • The final line uses echo to output the result.

But this only works for the "/dev" directory. How can we make the script work with any directory? All it takes is one small change.

Many commands, such as ls and wc , take command line parameters. These provide information to the command, so it knows what you want it to do. If you want ls to work on your home directory and also to show hidden files , you can use the following command, where the tilde ~ and the -a (all) option are command line parameters:

Our scripts can accept command line parameters. They're referenced as $1 for the first parameter, $2 as the second, and so on, up to $9 for the ninth parameter. (Actually, there's a $0 , as well, but that's reserved to always hold the script.)

You can reference command line parameters in a script just as you would regular variables. Let's modify our script, as shown below, and save it with the new name fcnt2.sh :

#!/bin/bashfolder_to_count=$1file_count=$(ls $folder_to_count | wc -l)echo $file_count files in $folder_to_count

This time, the folder_to_count variable is assigned the value of the first command line parameter, $1 .

The rest of the script works exactly as it did before. Rather than a specific solution, your script is now a general one. You can use it on any directory because it's not hardcoded to work only with "/dev."

Here's how you make the script executable:

chmod +x fcnt2.sh

chmod +x fcnt2.sh in a terminal window

Now, try it with a few directories. You can do "/dev" first to make sure you get the same result as before. Type the following:

./fnct2.sh /dev

./fnct2.sh /etc

./fnct2.sh /bin

./fnct2.sh /dev in a terminal window

You get the same result (207 files) as before for the "/dev" directory. This is encouraging, and you get directory-specific results for each of the other command line parameters.

To shorten the script, you could dispense with the variable, folder_to_count , altogether, and just reference $1 throughout, as follows:

#!/bin/bash file_count=$(ls $1 wc -l) echo $file_count files in $1

We mentioned $0 , which is always set to the filename of the script. This allows you to use the script to do things like print its name out correctly, even if it's renamed. This is useful in logging situations, in which you want to know the name of the process that added an entry.

The following are the other special preset variables:

  • $# : How many command line parameters were passed to the script.
  • $@ : All the command line parameters passed to the script.
  • $? : The exit status of the last process to run.
  • $$ : The Process ID (PID) of the current script.
  • $USER : The username of the user executing the script.
  • $HOSTNAME : The hostname of the computer running the script.
  • $SECONDS : The number of seconds the script has been running for.
  • $RANDOM : Returns a random number.
  • $LINENO : Returns the current line number of the script.

You want to see all of them in one script, don't you? You can! Save the following as a text file called, special.sh :

#!/bin/bashecho "There were $# command line parameters"echo "They are: $@"echo "Parameter 1 is: $1"echo "The script is called: $0"# any old process so that we can report on the exit statuspwdecho "pwd returned $?"echo "This script has Process ID $$"echo "The script was started by $USER"echo "It is running on $HOSTNAME"sleep 3echo "It has been running for $SECONDS seconds"echo "Random number: $RANDOM"echo "This is line number $LINENO of the script"

Type the following to make it executable:

chmod +x special.sh

fig13 in a terminal window

Now, you can run it with a bunch of different command line parameters, as shown below.

./special.sh alpha bravo charlie 56 2048 Thursday in a terminal window

Bash uses environment variables to define and record the properties of the environment it creates when it launches. These hold information Bash can readily access, such as your username, locale, the number of commands your history file can hold, your default editor, and lots more.

To see the active environment variables in your Bash session, use this command:

env | less in a terminal window

If you scroll through the list, you might find some that would be useful to reference in your scripts.

List of environment variables in less in a terminal window

When a script runs, it's in its own process, and the variables it uses cannot be seen outside of that process. If you want to share a variable with another script that your script launches, you have to export that variable. We'll show you how to this with two scripts.

First, save the following with the filename script_one.sh :

#!/bin/bashfirst_var=alphasecond_var=bravo# check their valuesecho "$0: first_var=$first_var, second_var=$second_var"export first_varexport second_var./script_two.sh# check their values againecho "$0: first_var=$first_var, second_var=$second_var"

This creates two variables, first_var and second_var , and it assigns some values. It prints these to the terminal window, exports the variables, and calls script_two.sh . When script_two.sh terminates, and process flow returns to this script, it again prints the variables to the terminal window. Then, you can see if they changed.

The second script we'll use is script_two.sh . This is the script that script_one.sh calls. Type the following:

#!/bin/bash# check their valuesecho "$0: first_var=$first_var, second_var=$second_var"# set new valuesfirst_var=charliesecond_var=delta# check their values againecho "$0: first_var=$first_var, second_var=$second_var"

This second script prints the values of the two variables, assigns new values to them, and then prints them again.

To run these scripts, you have to type the following to make them executable:

chmod +x script_one.shchmod +x script_two.sh

chmod +x script_one.sh in a terminal window

And now, type the following to launch script_one.sh :

./script_one.sh

./script_one.sh in a terminal window

This is what the output tells us:

  • script_one.sh prints the values of the variables, which are alpha and bravo.
  • script_two.sh prints the values of the variables (alpha and bravo) as it received them.
  • script_two.sh changes them to charlie and delta.
  • script_one.sh prints the values of the variables, which are still alpha and bravo.

What happens in the second script, stays in the second script. It's like copies of the variables are sent to the second script, but they're discarded when that script exits. The original variables in the first script aren't altered by anything that happens to the copies of them in the second.

You might have noticed that when scripts reference variables, they're in quotation marks " . This allows variables to be referenced correctly, so their values are used when the line is executed in the script.

If the value you assign to a variable includes spaces, they must be in quotation marks when you assign them to the variable. This is because, by default, Bash uses a space as a delimiter.

Here's an example:

site_name=How-To Geek

site_name=How-To Geek in a terminal window

Bash sees the space before "Geek" as an indication that a new command is starting. It reports that there is no such command, and abandons the line. echo shows us that the site_name variable holds nothing — not even the "How-To" text.

Try that again with quotation marks around the value, as shown below:

site_name="How-To Geek"

site_name=

This time, it's recognized as a single value and assigned correctly to the site_name variable.

It can take some time to get used to command substitution, quoting variables, and remembering when to include the dollar sign.

Before you hit Enter and execute a line of Bash commands, try it with echo in front of it. This way, you can make sure what's going to happen is what you want. You can also catch any mistakes you might have made in the syntax.

IMAGES

  1. How to use Variables in Bash

    bash variables yq

  2. How to Use Variables in Bash Shell Scripts

    bash variables yq

  3. How to use Variables in Bash Programming

    bash variables yq

  4. What Are Bash Variables and How Can You Use Them?

    bash variables yq

  5. What Are Bash Variables and How Can You Use Them?

    bash variables yq

  6. Bash Basics #2: Use Variables in Bash Scripts

    bash variables yq

VIDEO

  1. Best deliveries of the week, December 9-15

  2. SHIRIN MUTATTAUNA FASHIN BAKI KAN KARAR DA AFRICA TA KUDU TA SHIGAR DA ISRAEL A KOTUN ICG

  3. Hells Angels 30th Anniversary Party🍻 @Dark_Soul_Family #hellsangels

  4. THE ELITE SNOW BASH 100

  5. Cathie Wood: "Elon Musks Buying Twitter Gives TREMENDOUS Potential To Tesla Stock!"

  6. Sharvari Wagh, Rakul Preet Singh, Alaya F Sizzles At Kartik Aaryan Birthday Bash

COMMENTS

  1. Bash variable substitution into yq command

    Bash variable substitution into yq command Ask Question Asked 11 months ago Modified 11 months ago Viewed 5k times 2 Given a YAML file, example.yaml: node: sub_node: get_this: I'd like to get a variable containing get_this using Mike Farah's yq and the string sub_node

  2. Processing YAML Content With yq

    Overview Many programs and services need their configuration to be written down in a structured way. One of the ways to achieve this goal is using YAML files. In the Bash shell, we need tools to deal with YAML content from the command line or script. In this tutorial, we're going to learn about the yq utility. 2. yq Basics

  3. Pass bash variable in yq

    Pass bash variable in yq Ask Question Asked 3 I am trying to pass bash variable in yq test.yml configuration: Properties: corporate-url: https://stackoverflow.com/ temp = '.configuration.Properties.corporate-url' export $temp Value1=$ (yq '. [env ($temp)]' test.yml) expected output: https://stackoverflow.com/

  4. Env Variable Operators

    a: true Read numeric environment variable Running myenv="12" yq --null-input '.a = env (myenv)' will output a: 12 Read yaml environment variable Running myenv=" {b: fish}" yq --null-input '.a = env (myenv)' will output a: {b: fish} Read boolean environment variable as a string Running

  5. GitHub

    yq a lightweight and portable command-line YAML, JSON and XML processor. yq uses jq like syntax but works with yaml files as well as json, xml, properties, csv and tsv. It doesn't yet support everything jq does - but it does support the most common operations and functions, and more is being added continuously.

  6. Mastering YAML Processing in Command Line

    Before we begin using yq, we first need to install it. When you google yq though, you will find two projects/repositories. First of them, at https://github.com/kislyuk/yq is wrapper around jq - the JSON processor. If you're already familiar with jq you might want to grab this one and use the syntax you already know.

  7. My Cheatsheet for the "yq" Tool

    yq parses YAML files in the similar way to tree parsing, dividing the YAML files into different node types, which are nested in one another. The three node types are Scalar, the common node, such as .metadata.name, whose value can be string, int, boolean, etc. Array, the array node, such as .spec.containers and .spec.volumes are both arrays.

  8. Variable Operators

    Variable Operators - yq GitBook Variable Operators Like the jq equivalents, variables are sometimes required for the more complex expressions (or swapping values between fields). Note that there is also an additional ref operator that holds a reference (instead of a copy) of the path, allowing you to make multiple changes to the same path.

  9. yq

    yq. a lightweight and portable command-line YAML processor. yq uses jq like syntax but works with yaml files as well as json. It doesn't yet support everything jq does - but it does support the most common operations and functions, and more is being added continuously. yq is written in go - so you can download a dependency free binary for your ...

  10. yq : A command line tool that will help you handle your YAML resources

    What can it do? yq can take a YAML file as an input and: Read values from the file. Add new values. Update Existing values. Generate new YAML files. Convert YAML into JSON. Merge two or more YAML files. Installation You can install yq on Mac OS with: $ brew install yq On Linux with:

  11. Tips, Tricks, Troubleshooting

    The best way to run a diff is to use yq to normalise the yaml files and then just use diff. Here is a simple example of using pretty print -P to normalise the styling and running diff: diff < (yq -P 'sort_keys (..)' -o=props file1.yaml) < (yq -P 'sort_keys (..)' -o=props file2.yaml) This way you can use the full power of diff and normalise the ...

  12. Set and Test Enviornment Variables in .yaml files in Linux

    I'm passing the variable via the environment and reading their values with strenv() inside the yq expression. This means that both variables have to be exported or, in some other way, made available to the yq process' environment. This yq utility can also do "in-place" edits if given the -i option. The output, given the document in the question:

  13. Guide to Passing Bash Variables to jq

    We can pass Bash variables to a jq program as a pre-defined named variable using the -arg option: $ jq --arg jq_var $ {bash_var} [options...] filter [files ...] First, let's add sample JSON data in the fruits_template.json file: $ cat fruits_template.json { "fruit": "Apple" , "size": "Large" , "color": "Red" } Copy

  14. Bash variables · Issue #928 · mikefarah/yq · GitHub

    Bash variables #928 Closed kamzar1 opened this issue on Aug 28, 2021 · 3 comments kamzar1 enhancement v4 mikefarah closed this as completed single quote sufficient: .'$user'.'$inst'.backups.'$bkname' singlequote, escape, doublequote: {"size": '\"$ {size}\"' , "created_at": '\"$ {date}' '$ {hour}\"'} plain (as expected) $yaml.file

  15. I cannot use bash variables anymore. · Issue #606 · mikefarah/yq

    Describe the bug I used yq like this yq eval -i config.yaml foo "$ {bar}" With yq eval, I cannot use anymore bash variables in expressions: foo=bar yq eval ".foo = $foo" toto/toto.yaml Error: '=' expects 2 args but there is 1 For info, jq...

  16. Create

    If a key or value has leading dashes, yq won't know that you are passing a value as opposed to a flag (and you will get a 'bad flag syntax' error). To fix that, you will need to tell it to stop processing flags by adding '--' after the last flag like so: yq n -t -- --key --value. Will result in ` --key: --value. Previous.

  17. yaml

    I have a yml file, from which I am obtaining a key with yq and I am storing it in a variable in bash, I am trying to do another query with the value of the variable but it does not give the expected result file.yml

  18. How to Work with Variables in Bash

    Home Linux How to Work with Variables in Bash By Dave McKay Updated Aug 12, 2023 Want to take your Linux command-line skills to the next level? Here's everything you need to know to start working with variables. Hannah Stryker / How-To Geek Readers like you help support How-To Geek.

  19. how to handle variables in bash for mike yq v4

    1 How can I use Mike Farah's YQ v4 to update a field that has special characters. e.g. manipulating below: containers: - name: flyway image: xx.dkr.ecr.eu-west-1.amazonaws.com/testimage:60 Errors for attempts: