Guide to Passing Bash Variables to jq

Last updated: March 18, 2024

assign jq result to variable bash

1. Overview

jq is a de facto standard utility for working with JSON data in Linux.

In this tutorial, we’ll learn how to make the jq program reusable by passing Bash variables to it .

2. Using the –arg Option

We can pass Bash variables to a jq program as a pre-defined named variable using the –arg option :

First, let’s add sample JSON data in the fruits_template.json file:

We must note that we plan to reuse the fruits_template.json file throughout this tutorial .

Further, let’s assume that we want to override the value of the fruit field with a user-defined value that’s available through the bash_fruit_var Bash variable:

Next, let’s go ahead and write a jq program using the –arg option to display the fruit field from the bash_fruit_var variable and all other field values from the fruits_template.json file using the .field operator :

We must note that we’ve added the bash_ and jq_ prefixes to the variable names to indicate that the former is a Bash variable while the latter is a jq variable. Further, we shall remember that we need to reference a jq variable by adding the $ prefix .

Finally, let’s also see an alternate approach to accessing the jq_fruit_var through $ARGS.named :

Great! We got this right. Further, we must note $ARGS.named only holds the named argument variables , as applicable in this case.

3. Using $ENV or the env Function

We can also pass the Bash variables as environment variables to the jq program. Subsequently, we can reference them within the jq program using either the $ENV variable or the env function .

Let’s start by using the $ENV variable to access the bash_fruit_var from the environment :

We must note that we could also export the bash_fruit_var to pass it as an environment variable to the jq program.

Next, let’s use the env function to access the bash_fruit_var from the environment :

Great! We’ve got the same result. Further, we must note that $ENV as an object represents the environment variables set when the jq program started, whereas env outputs an object representing jq ‘s current environment .

4. Using the –args Option

Alternatively, we can also pass the Bash variables as positional arguments to the jq program using the –args option :

We must note that jq will treat all the arguments after –args as positional arguments, so we must place it after the filename .

Let’s go ahead and define two Bash variables, namely bash_fruit_var and bash_color_var :

Moving on, let’s pass bash_fruit_var and bash_color_var variables to the jq program to override the fruit and color fields in the fruits_template.json file :

We must note that $ARGS.positional only holds positional argument variables with zero-based indexing .

5. Using the –argjson Option

We can also pass JSON-encoded Bash variables to the jq program by using the –argjson option :

Let’s define the bash_fruit_json Bash variable to hold a JSON object with two fields, namely fruit  and color :

In continuation, let’s pass the Bash variable bash_fruit_json as the jq variable jq_fruit_json and use it to override the values of  fruit and color fields in the fruits_template.json file :

Finally, let’s rewrite our program to access the jq_fruit_json variable through $ARGS.named :

Perfect! The result is as we expected. Further, we must note that enclosing the Bash variable within double quotes is mandatory when it contains JSON-encoded values .

6. Using the –jsonargs Option

We can use the –jsonargs option for passing JSON-encoded Bash variables as positional arguments :

Similar to the position of the –args option, we must remember to place the –jsonargs after the filename because jq will interpret all the values after –jsonargs as positional arguments .

Let’s go ahead and pass bash_fruit_json as a positional argument to the jq program:

The output looks correct. Further, we must note that we used the $ARGS.positional object to access the JSON-encoded data passed to the program .

7. Using the –rawfile Option

We can use the –rawfile option to pass the contents of a file as a variable to the jq program :

We must note that while $ bash_path_var is a Bash variable representing a valid file path, jq_file_contents is a jq variable containing the contents from that file .

Let’s start by taking a look at the raw_fruits_data file containing the name and color information for multiple fruits:

Next, let’s define the bash_fruits_file Bash variable and initialize it to the raw_fruits_data file path:

We must note that storing the file path in a Bash variable allows us to reuse the same jq program in a Bash script.

Moving on, let’s write a jq program to transform the raw data available in the raw_fruits_data file into multiple JSON fruit objects by passing its content in the jq_raw variable:

We must note that we stored the content of the fruits_template.json file in the fruit_json variable and applied the split function over the jq_raw variable to get an array of strings. Further, we used the iterator operator ( .[] ) and the select function to filter out the empty values.

Generally speaking, using the –rawfile option, we can pass non-JSON data directly from a file to the jq program.

8. Using the –slurpfile Option

We can use the –slurpfile option to pass the contents of a file containing JSON-encoded data as an array to the jq program :

Let’s take a look at the my_fruit_jsons  file containing multiple JSON objects:

We must note that the my_fruit_jsons file itself isn’t a valid JSON array object .

Next, let’s define the bash_fruits_json_file Bash variable and initialize it to the my_fruit_jsons  file path:

We must note that storing the file path in a Bash variable allows us to reuse the same jq program in a script.

Next, let’s see this in action by binding the JSON objects to the my_fruits array object in the jq program :

We can notice that the output is a valid JSON object now. Further, we must note that the size field is missing, as it’s not present in the individual JSON objects.

Moving on, let’s write a jq program to transform the JSON objects available in the my_fruit_jsons file into raw text separated by a | character :

We must note the content of the fruits_template.json file is available in the $fruit_json variable. Moreover, we used the reduce function to generate pipe-separated values, each separated with the newline character (“\n”) .

Later, we used the split function followed by the select function to filter out the empty line by iterating over individual string values. Additionally, we used the –raw-output option to show the output as raw text .

9. Using Variables Within Filter

Let’s start by defining the Bash variable bash_field_var and using it intuitively to get a specific field from the JSON object in fruits_template.json :

We can notice that we get a syntax error. However, the error also suggests the right way to access the field using the variable.

Next, let’s use [$jq_field] with the .field operator to access the fields represented by the bash_field_var Bash variable :

Great! We got the correct result this time.

10. Conclusion

In this tutorial, we learned multiple ways to pass Bash variables to a jq program . Additionally, we explored different options with the jq command, such as –arg , –args , –argjson , –jsonargs , –rawfile , and –slurpfile , along with a few functions such as split and reduce .

Working with JSON in bash using jq

Jq is a powerful tool that lets you read, filter, and write json in bash.

The logos for bash and JSON next to each other

Want the TL:DR? See the jq cheatsheet

Perhaps you’ve seen or even written bash that looks like this:

(Note: the above code was taken from https://hackernoon.com/a-crude-imessage-api-efed29598e61 , which is a great article).

That’s tough to read and even tougher to write. You have to pipe to 4 different utilities just to get to a property in the JSON response body! Bash doesn’t understand JSON out of the box, and using the typical text manipulation tools like grep, sed, or awk, gets difficult. Luckily there’s a better way using a tool called jq .

jq can simplify the above bash to this:

That’s much nicer 😎. By making JSON easy to work with in bash, jq opens up a lot of automation possibilities that otherwise required me to write something in node.js (which isn’t bad, it just takes longer generally).

Why not just use node.js when you need to deal with JSON?

Sometimes node.js is the right tool. For most automation tasks, I like to use bash whenever possible because it’s faster and even more portable (I can share a bash script with team members that don’t have node.js installed). To me, bash is more expressive and succinct for certain tasks than node is.

jq isn’t a built-in command in any environment, so you have to install it. Run brew install jq on macOS. See jq’s install help page for how to install on other environments.

Basics of jq

jq works similarly to sed or awk — like a filter that you pipe to and extract values from. Also like sed or awk, it basically has it’s own domain specific language (DSL) for querying JSON. Luckily, it’s really intuitive (unlike awk 😜).

Get a property

Let’s say we have JSON that looks like this:

To print out the foo property, we use the . operator followed by the property name.

That will print out 123 , as expected.

This works with nesting too. So .a.b.c.d will traverse down nested objects’ properties.

This, all by itself, is pretty useful. For a realistic and totally useful example, let’s write a script that gets the Astronomy Picture of the Day and sets it as our wallpaper (this is macOS only).

Yay! All this astronomy stuff makes it feel like the right time for a Neil deGrasse Tyson gif.

Note that if a property has a spaces or weird characters in it, you’ll have to use quotes. For example:

Also, be sure to always wrap your jq selector in a single-quotes, otherwise bash tries to interpret all the symbols like . , whereas we want jq to do that.

Now let’s see how iteration works. The array or object value iterator operator, .[] , is what makes it possible.

Here’s a really basic example:

That will output 1, 2, 3 on separate lines.

In an array of objects, you can access a property on each item in the array like so:

Or on an object, .[] will output the value of each key/value pair:

So that will return 1 2.

Note that you can also pass an index to .[] , so

will return just bar.

Now how do we do something for each line? In the same way you’d handle anything that outputs multiple lines of information in bash: xargs , for loops, or some commands just handle multiple input items, etc. For example:

jq Functions

jq also has built-in “functions”. Returning to the previous object iteration example — let’s say we wanted get the keys of the object (not the values) as an array:

which will return a b . Note that we’re also using the pipe | operator, which works the same in jq as it does in bash — it takes the output from the left and passes it as input to the right.

Another handy function for arrays and objects is the length function, which returns the array’s length property or the number of properties on an object.

You can get even fancier and create intermediary arrays and objects on the fly. Here, I’m combining the keys of the dependencies and devDependencies objects (from a package.json file) into an array, flattening it, and then getting the length.

That returns the number of dependencies and devDependencies a package.json contains.

Creating objects

You can also create objects on the fly. This can be useful for re-shaping some JSON. For example:

Let’s use it for real now

What if I wanted to audit my package.json dependencies and remove anything that’s not being used? Unused dependencies slow down npm installs for everyone and is just messy. I could manually grep usages of each dependency (via grep or in my IDE), but if you have a lot of dependencies that gets tedious fast, so let’s automate it.

[1] Here’s how the grep flags work:

–include and –exclude-dir narrow the files that get searched

-R means recursive, tells it to grep all matching files

–color colorizes the output

-n displays line numbers

[2] I have to export it so that a subshell can see it. If you want xargs to call a custom function, you have to call it in a subshell for some reason

[3] -r is for “raw-output”, so no quotes around values, which makes it suitable for processing in other bash commands. We get the dependency names as an array (this is equivalent to Object.keys(require(‘./package.json’).dependencies) in node.js)

[4] Then we pipe that to xargs which handles setting up a grep for each lines. Here’s how the xargs flags all work:

-t tells it to echo the constructed command; useful for debugging

-I {} defines the replacement string where the dependency string will get placed

-P 4 defines the concurrency, so 4 concurrent greps

we tell it to start a bash subshell where our grep_dep function is called with it’s args

There’s more that could be done to the grep-ing in that script to make it more robust, but that’s the basic gist.

I used something similar to this recently at work to prune unused dependencies. We have a huge front-end monolith with a single package.json that has 250 dependencies 🙀, so some automated assistance was necessary.

jq is awesome and makes working with JSON in bash easy. The DSL for filtering, querying, and creating JSON goes much deeper than what I’ve covered here, so see https://stedolan.github.io/jq/manual/ for the full documentation.

Share article

Using bash variables in jq

Due to the special characters used in json, the easiest way to use jq with inline scripts it by putting it between single quotes. That makes it impossible to use bash variables inside your script . Fortunately, jq has an --arg parameter to create a predefined variable from an external source.

You can use it to define a $foo variable with the contents of bash variable $FOO , for example:

You might notice the -n parameter: This tells jq to use null as input instead of reading json from stdin like it normally would. I'm using it in all examples on this page as it makes them a bit shorter. In real world usage you probably won't need it.

As usual in bash scripts you can also execute a command inline:

Every variable created with --arg is treated as a string . That might cause some unexpected behaviour when you work with numbers, like when you attempt to add a number to an argument. Comparisons between strings and numbers seem to work fine, but I wouldn't count on it (pun intended).

There are two ways to solve this: either convert the variable to a number with tonumber or use --argjson instead. I prefer to use tonumber because that way I'm sure jq forces it to be a number. As my argument may be the result of an earlier command, I may be using an error message as input instead of the expected numeric result.

Variables from files

It's also possible to load variables from files. With --slurpfile we can read a file containing json objects. An array containing the separate objects is then made available as a predefined variable. Note that even if your file contains only a single json object, as the variable is still an array, the [0] is required to access it.

A second option is --rawfile , creating a string variable with the exact contents of the file. Note that as jq outputs json, reading and printing the file from the previous example results an escaped string.

assign jq result to variable bash

  • Latest Articles
  • Top Articles
  • Posting/Update Guidelines
  • Article Help Forum

assign jq result to variable bash

  • View Unanswered Questions
  • View All Questions
  • View C# questions
  • View C++ questions
  • View Javascript questions
  • View Visual Basic questions
  • View Python questions
  • CodeProject.AI Server
  • All Message Boards...
  • Running a Business
  • Sales / Marketing
  • Collaboration / Beta Testing
  • Work Issues
  • Design and Architecture
  • Artificial Intelligence
  • Internet of Things
  • ATL / WTL / STL
  • Managed C++/CLI
  • Objective-C and Swift
  • System Admin
  • Hosting and Servers
  • Linux Programming
  • .NET (Core and Framework)
  • Visual Basic
  • Web Development
  • Site Bugs / Suggestions
  • Spam and Abuse Watch
  • Competitions
  • The Insider Newsletter
  • The Daily Build Newsletter
  • Newsletter archive
  • CodeProject Stuff
  • Most Valuable Professionals
  • The Lounge  
  • The CodeProject Blog
  • Where I Am: Member Photos
  • The Insider News
  • The Weird & The Wonderful
  • What is 'CodeProject'?
  • General FAQ
  • Ask a Question
  • Bugs and Suggestions

Jq - assign variable from jq output of length of array

assign jq result to variable bash

2 solutions

  • Most Recent

Add your solution here

Your Email  
Password  
Your Email  
?
Optional Password  
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Print

Top Experts
Last 24hrsThis month
175
70
20
10
10
610
455
340
190
170

assign jq result to variable bash

Assigning MySQL Dump Output

Assigning Result of mysqldump Command to a Variable

Abstract: Learn how to assign the output of an mysqldump command to a variable in a Bash script.

Assigning Result to mysqldump Command Variable

Introduction.

In this article, we will discuss how to assign the result of a mysqldump command to a variable in a Bash script. This is a common practice when backing up a MySQL database using a script. We will cover the key concepts, subtitles, and provide detailed context on the topic.

Key Concepts

  • mysqldump command
  • Variable assignment in Bash
  • Redirection operator >
  • Backticks and $() command substitution

The mysqldump Command

Variable assignment in bash, redirection operator >, backticks vs. $() command substitution, putting it all together.

mysqldump is a command-line utility that comes with the MySQL database server. It is used to create a backup of a MySQL database in a format that can be easily restored later. The basic syntax of the mysqldump command is as follows:

In the above command, dbname is the name of the database that you want to backup, and backupfile.sql is the name of the file where the backup will be saved. The > symbol is a redirection operator that redirects the output of the mysqldump command to the specified file.

In Bash, you can assign the output of a command to a variable using the = operator. For example, the following command assigns the output of the date command to the variable now :

The redirection operator > is used to redirect the output of a command to a file. For example, the following command redirects the output of the echo command to the file hello.txt :

There are two ways to perform command substitution in Bash: backticks and $() . Both of these methods allow you to assign the output of a command to a variable. However, the $() method is preferred over backticks because it is easier to read and less prone to errors.

Here's an example of command substitution using backticks:

And here's the same example using $() :

Now that we have covered the key concepts, let's put it all together in an example Bash script:

In the above script, we first set the variables for the database user, password, name, destination directory, and filename. We then use the $(...) command substitution to assign the result of the mysqldump command to the dumpresult variable. Finally, we check the exit status of the mysqldump command to determine if the dump was successful.

In this article, we have discussed how to assign the result of a mysqldump command to a variable in a Bash script. We covered the key concepts of the mysqldump command, variable assignment in Bash, redirection operator > , and backticks vs. $() command substitution. We also provided a detailed example of how to put it all together in a Bash script.

  • MySQL Documentation: mysqldump — A Database Backup Program
  • GNU Bash Manual: 3.5.4 Command Substitution
  • Redirection - Greg's Wiki

HTML Unordered List

In this software development article, we will explain how to assign the result of an mysqldump command to a variable in a bash script. this can be useful for automating database backups or other database-related tasks. to get started, let's take a look at the mysqldump command and how it's typically used: ```bash mysqldump --user= --password= > / ``` this command dumps the contents of a mysql database into a file. however, if we want to assign the output of the command to a variable instead, we can use command substitution: ```bash dump_result=$(mysqldump --user= --password= > / ) ``` the output of the command is stored in the variable `dump_result`. this variable can then be used in further scripting or processing. that's it for this quick article on assigning the result of an mysqldump command to a variable in bash. stay tuned for more software development tips and tricks, setting json data metadata in a gcp project.

Learn how to set JSON data meta-data in a Google Cloud Platform (GCP) project using the gcloud command-line tool.

Simplifying Multiple SELECTs with Firebird Stored Procedures: A Case Study

In this article, we explore how to optimize multiple SELECT queries in Firebird by using stored procedures. We'll discuss a real-life scenario where a particular SP ends up being called multiple times, and how we can simplify the code by encapsulating the logic in a stored procedure.

Playwright Tests: Console.log Statements Not Outputting Logs in Debug Mode

This article discusses an issue where console.log statements in Playwright tests written in Typescript do not output logs during debug mode.

Customizing Error Highlighting in VSCode According to ESLint Settings

Learn how to configure error highlighting and auto-corrections in Visual Studio Code (VSCode) according to your ESLint settings.

Tags: :  Databases Scripting

Latest news

  • Issue with LinearProgress and CircularProgress components in Material-UI Next.js 12 application
  • Potential Interference of Data Flow in Google Play Store Applications
  • Laravel 10, Livewire 2: AJAX Component 'search-suggest-dropdown' doesn't work consistently, giving 'Error evaluating filteredItems'.
  • Error Installing scikit-learn on Python 3.12.3
  • Strange Behavior of Flutter Web App: URL Change after Release to live.localhost works fine.
  • Adding Custom Classes to Splide.js Carousel: splide__slide
  • Relative Paths Not Working in VSCode: A Solution for React Apps
  • Get Specific Tail Length of Square Waves using NumPy: A Comprehensive Guide
  • Comparing Presence/Absence of Four Different Pathogens in Edible Crabs at Different Sampling Locations: A Two-Season Study
  • Macro for Getting Date Taken of Pictures and Videos: A Solution
  • Troubleshooting File-in-Use Errors with PowerShell's Out-File
  • Alignment Problem: Solving UBO Size Issues in GLSL and Vulkan
  • React Native: 'init' Method Giving Error - EBUSY: resource busy
  • Implementing Multi-Select Drop-Down Lists with Stored Values in .NET 8 Forms
  • Decomposing ECDH Public Keys: Shared Key Generation
  • Circle Detection and Image Mask Implementation using Python
  • Web Scraping Takes Long Time, Gives Error: A PHP Perspective
  • Unwanted Translucent Overlay Appears on Android Screens During App Navigation
  • Django ORM QueryString Treats Model Choice Value as Column
  • Handling Missing Notices in Python/XML Scripts
  • Resolving 'Service Unavailable' Error during FedEx Image Upload via REST API
  • Unable to Load Google Maps JS API in Ionic/Capacitor using Physical Device Emulator
  • XMonad: Fixing US Keyboard Layout Shortcuts in Ubuntu for Firefox
  • Lock-Based Concurrent Vector in STL: Thread Safety with mutex and std::vector
  • Lint Recommends Using Standard Guava for Android Projects: A Gradle Perspective
  • Creating a Custom Field: OrderType in Shopware Administration Panel
  • YugabyteDB 2.14.2.0: Handling Concurrent DDL with Non-Colocated Tables
  • Sampling Rows and Whole Groups in DataFrame: A Practical Example
  • Flutter iOS Simulator Failed to Run: Troubleshooting Steps
  • Verifying XML Signatures with Apache Santuario: Checking Signature Validity in Encrypted Files
  • Custom Print Format Attached upon Saving ERPNext Quotation Doctype
  • Retrieving Unique Job IDs in Google Cloud Run using Java
  • Issue Updating Values in Memory Game: Turning Cards
  • Avoid Bootstrap Labels Overflow with Datepicker in .NET 8
  • Vim: Creating a New File and Treesitter not working for Filetype?

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

What's the shortest way of making jq read from a variable without using a pipe?

Let's say I have following bash script:

I'm basically using the Nominatim API to get the name of a road in a specific location... The result is:

I'm using process substitution with <(echo "$json") in order to use the value of the $json variable with jq ... However, I feel that's not the most straightforward way of doing it. I've tried searching for a parameter on man jq but I didn't manage to find what would do that for me. In pseudocode I want something like:

Is there any jq parameter that allows me to do that? Or does it only work with files or pipes and using process substitution is the workaround in this case?

raylight's user avatar

  • 1 see Can I pass a string variable to jq not the file? on SO. –  αғsнιη Commented Apr 22, 2022 at 4:11
Or does it only work with files or pipes and using process substitution is the workaround in this case?

Well, it works with files or standard input; pipes is a way of using standard input and process substitution is a way of using files. You could use heredocs or herestrings for standard input as well.

That said, you can use --argjson :

So, in your case, you could do:

But to me personally, the most straightforward way of doing it would be to pipe curl to jq :

Kusalananda's user avatar

  • Thanks, in my particular case I was avoiding using the pipe because I need to get multiple parameters from this JSON... So I'd need to use more lines like postcode="$(echo "$json" | jq '.address.postcode)" . The option with jq '.address.postcode' <<< "$json" from the link on the comments I didn't know and is a straightforward option too... –  raylight Commented Apr 22, 2022 at 5:00
  • 2 Yes, that's a "herestring". Personally, though, I don't see anything wrong with echo "$json" | jq ... . It's pretty easy to understand and isn't particularly worse off compared to jq ... <<<"..." . No special syntax needed, portable to sh . –  muru Commented Apr 22, 2022 at 5:25
  • 1 Calling jq to parse a document several times, and having to store JSON in shell variables seems a bit of a waste. eval "$(curl ... | jq -r '.address | @sh "postcode=\(.postcode)", @sh "road=\(.road)"')" . If eval seems bad, just output to a tab-delimited list and read with IFS=$'\t' read -r postcode road . –  Kusalananda ♦ Commented Apr 22, 2022 at 6:11

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged jq ..

  • Featured on Meta
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
  • We spent a sprint addressing your requests — here’s how it went

Hot Network Questions

  • As a DM, what should I do if a person decides to play a rogue?
  • Could a Black Market exist in a cashless society (digital currency)?
  • Problem computing a limit
  • Implementation of Euler-Maruyama numerical solver
  • Olympic basketball terms: what does “gutted on the glass and in the paint” mean?
  • Distorted square wave
  • Is it possible with modern-day technology to expand an already built bunker further below without the risk of collapsing the entire bunker?
  • Book about aliens coming back to Earth to recover a lost spaceship
  • How should I deal with curves in new deck boards during installation?
  • How does light beyond the visible spectrum relate to color theory?
  • Flyback Diode Forward Voltage
  • Is it a security issue to expose PII on any publically accessible URL?
  • In the travel industry, why is the "business" term coined in for luxury or premium services?
  • How can I fix this rust on spokes?
  • Are there countries where only voters affected by a given policy get to vote on it?
  • o y u (or and or)
  • How to delete an island whose min x less than -1
  • Is an employment Conflict of Interest necessary when redundant with my Affiliation?
  • Meaning of output from fast-chess engine testing suite
  • Can a festival or a celebration like Halloween be "invented"?
  • Good postdoc position, but how to approach advisor about feelings of self-doubt?
  • Accommodating whiteboard glare for low-vision student
  • Error concerning projectile motion in respected textbook?
  • Typical password generator in Python

assign jq result to variable bash

  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

How to assign the result of mysqldump command into a variable?

In my bash script, I have tried

to get the result of the mysqldump command into a variable (in case of an error). However $dumpresult will remain empty. Any idea?

To make it clear, I need the mysqldump in a file (that works fine already) and the mysqldump error messages of stderr in $dumpresult.

Thanks a lot,

Gary U.U. Unixuser's user avatar

You assign the standard output of the command to a variable, but since you redirect the stdout to a file, there is no stdout at all.

You can verify this by doing just a

You will see that nothing is printed. Consequently, if you would assign the output to your variable, the variable would be empty.

To get the stdout to a file and into your variable, you have to do one of these:

If you are interested in the stderr of the mysqldump , I suggest to use

user1934428's user avatar

  • Sorry, when I didn't make it clear: I need the normal mysqldump output into a file > ${desdir}/${filename_sql} -- that works fine already. –  Gary U.U. Unixuser Commented 1 hour ago
  • However, when mysqldump encounters an error, it is routed to stdout, like "mysqldump: Got error: 1049: "Unknown database 'abc'" when selecting the database". I just want to get the stdout error messages into the variable. Thx –  Gary U.U. Unixuser Commented 1 hour ago
  • @GaryU.U.Unixuser: I'm not a regular mysql user, but from what I know, the error messages are written to stderr, and you don't catch stderr anywhere. I will edit my answer to cope with this aspect (you really should have written in your question that you are interested in stderr).... –  user1934428 Commented 1 hour ago

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Sign up or log in

Post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

Not the answer you're looking for? Browse other questions tagged bash shell reroute or ask your own question .

  • Featured on Meta
  • We spent a sprint addressing your requests — here’s how it went
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network...
  • What makes a homepage useful for logged-in users

Hot Network Questions

  • What do American people call the classes that students go to after school for SATs?
  • Time dilation in Einstein's train example (lightning strike)
  • Are there other proposed translations of "aelfheres" in Beowulf than a name?
  • What does "I'll do, I'll do, and I'll do" mean?
  • Histogram manipulation
  • Are there any reasons I shouldn't remove this odd nook from a basement room?
  • Good postdoc position, but how to approach advisor about feelings of self-doubt?
  • Does there exist a nontrivial "good" set?
  • Book about aliens coming back to Earth to recover a lost spaceship
  • Eliminate some numbers so that each of the three rows contains the numbers 1 through 9 each exactly once
  • Dual of slope semistable vector bundle on higher dimensional variety
  • Are Artificers subject to the same magic item restrictions as other characters in AL?
  • Implementation of Euler-Maruyama numerical solver
  • Problem computing a limit
  • In the travel industry, why is the "business" term coined in for luxury or premium services?
  • Sci-fi movie - men in a bar in a small town, trapped by an alien
  • French Election 2024 - seat share based on first round only
  • My result is accepted in a journal as an errata, but the editors want to change the authorship
  • A web site allows upload of pdf/svg files, can we say it is vulnerable to Stored XSS?
  • How should I deal with curves in new deck boards during installation?
  • Confusion regarding "since" vs "for"
  • Distorted square wave
  • Who is ??? In Cult of the Lamb?
  • Looking for title of old Star Trek TOS book where Spock is captured and gets earring

assign jq result to variable bash

IMAGES

  1. Unix & Linux: Setting jq output to a Bash Variable (3 Solutions!!)

    assign jq result to variable bash

  2. How to work with JSON in BASH using jq?

    assign jq result to variable bash

  3. Bash jq command

    assign jq result to variable bash

  4. Array : Assigning an Array Parsed With jq to Bash Script Array

    assign jq result to variable bash

  5. How to use Variables in Bash

    assign jq result to variable bash

  6. How to Assign Variable in Bash Script? [8 Practical Cases]

    assign jq result to variable bash

VIDEO

  1. Python Programming : MCQ Quiz 2

  2. Math 2A. Calculus. Lecture 15. Implicit Differentiation

  3. How to Group Likert Scale Data in SPSS

  4. Avatar Live Action Versus🔥

  5. FE Review Mathematics

  6. TOP NOTCH TRUCKERS INC MTC 23

COMMENTS

  1. Setting jq output to a Bash Variable

    The present solution with read is useful when you need to assign multiple keys to multiple shell variables in one shot; you then just need to use read var1 var2 var3 < <(...). Here are more general snippets to assign jq output to shell variables, that can be very handy. Two methods for single-line input (without loop):

  2. jq: How do I assign a jq output to a variable?

    jq: How do I assign a jq output to a variable? Ask Question Asked 1 year, 10 months ago. Modified 1 year, 10 months ago. Viewed 5k times ... How to extract value from json contained in a variable using jq in bash. 0. JQ : Output with static value / variable. 0. JQ Not Taking In Variable Data. 1.

  3. How to make jq output into a variable

    Solved Note: a better way of doing this is saving the curl input as json and then using that with jq so you dont have to request stuff multiple times (sorry if this is a dumb question im new to bash and jq)

  4. Guide to Passing Bash Variables to jq

    Further, let's assume that we want to override the value of the fruit field with a user-defined value that's available through the bash_fruit_var Bash variable: $ bash_fruit_var=Banana. Next, let's go ahead and write a jq program using the -arg option to display the fruit field from the bash_fruit_var variable and all other field values from the fruits_template.json file using the ...

  5. Parsing JSON output to variable in bash using jq filters

    Parsing JSON output to variable in bash using jq filters. Ask Question Asked 1 year, 6 months ... Getting the result into a shell variable is then a matter of running the pipeline in a command substitution and assigning it to a variable: ... Look at the result of the curl request and whether the response looks very different from what you ...

  6. Working with JSON in bash using jq

    Also, be sure to always wrap your jq selector in a single-quotes, otherwise bash tries to interpret all the symbols like ., whereas we want jq to do that. Iteration. Now let's see how iteration works. The array or object value iterator operator, .[], is what makes it possible. Here's a really basic example: echo '[1, 2, 3]' | jq '.[]'

  7. Using bash variables in jq

    Due to the special characters used in json, the easiest way to use jq with inline scripts it by putting it between single quotes. That makes it impossible to use bash variables inside your script. Fortunately, jq has an --arg parameter to create a predefined variable from an external source.

  8. jq

    Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site

  9. Jq

    I have a bash shell script that performs a cURL command, and gets the raw output of an array inside the json array. I can also successfully echo the number of elements in the array Why can I not assign the amount of elements inside the array to a variable? it is always blank

  10. JSON array to bash variables using jq

    As ever when using eval, be careful that you trust the data you're getting, since if it's malicious or just in an unexpected format things could go very wrong.In particular, if the key contains shell metacharacters like $ or whitespace, this could create a running command. It could also overwrite, for example, the PATH environment variable unexpectedly.

  11. How to call jq in bash to store returned value in variable

    jq '.defines[] | select(.id==2)' source.json I see desired output. I am quite new to bash scripting, so I can only guess that it's not jq part that is the source of problem, but the way I am trying to call it in bash.

  12. Assigning Result of mysqldump Command to a Variable

    In this article, we have discussed how to assign the result of a mysqldump command to a variable in a Bash script. We covered the key concepts of the mysqldump command, variable assignment in Bash, redirection operator >, and backticks vs. $() command substitution. We also provided a detailed example of how to put it all together in a Bash script.

  13. bash

    This calls curl once and passes the resulting document through a single jq invocation without having to store it in a file or variable. The jq expression creates three strings. Each string is a variable assignment. The @sh operator in jq makes sure that the string is properly quoted for the shell.

  14. json

    This is the only 100%-safe answer; it lets jq properly create the filter using the value, rather than using bash to create a string that jq interprets as a filter. (Consider what happens if the value of EMAILID contains a ) .)

  15. Assigning an Array Parsed With jq to Bash Script Array

    Use jq -r to output a string "raw", without JSON formatting, and use the @sh formatter to format your results as a string for shell consumption. Per the jq docs: @sh: The input is escaped suitable for use in a command-line for a POSIX shell. If the input is an array, the output will be a series of space-separated strings.

  16. What's the shortest way of making jq read from a variable without using

    Or does it only work with files or pipes and using process substitution is the workaround in this case? Well, it works with files or standard input; pipes is a way of using standard input and process substitution is a way of using files.

  17. Can I pass a string variable to jq rather than passing a file?

    Store the result in a variable to use in later stages. Say your json is already in a variable and you need the result in another one. jsonData="{"key":"value"} ... Pass bash variable (string) as jq argument to walk JSON object. 3. Passing multiple variables not files to jq command. 1.

  18. jq

    1. If both the input and map objects are in separate files we can use an alternative to Botje 's solution, were we read both object into a single array using the -s (slurp) option. Then we can. jq -s '.[1].type = .[0][.[1].type] | last' map input. Change .type (on second index) to the mapped value from the first index.

  19. bash

    In my bash script, I have tried dumpresult=$(mysqldump --user="${dbuser}" --password="${dbpw}" ${dbname} > ${desdir}/${filename_sql}) and dumpresult=`mysqldump --user="$