I have a couple of hours (I started today) experience in Bash. I have been trying multiple times to modify the value from a key in a json file using it. I know that it's much easier in python but I was asked to do this with a bash script.

The thing is the value to modify is a path that uses slash: and I did this (first to change the contents and then to save it as the same file):

contents="$(jq ".out_directory = '$new_out_directory'" $new_json_path)" && \
echo -E "${contents}" > $new_json_path


  • .out_directory, the json parameter key given
  • $new_out_directory is file path "a/path/like/this"
  • $new_json_path is the path where the json file is saved

I believe that my problem happens here:

".out_directory = '$new_out_directory'" because this gives me

jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at , line 1: .out_directory = '/data/storage1/diego/trained_models/4959952e12a05516cef25c76668d9338'

but this: ".out_directory = $new_out_directory" produces:

jq: error: syntax error, unexpected '/' (Unix shell quoting issues?) at , line 1: .out_directory = /data/storage1/diego/trained_models/40a8211dc942414a7bf966dc668d938b

and this (the logical solution) '.out_directory = "$new_out_directory"' saves literally $new_out_directory as a key:


but I'm looking for something like this

out_directory: /the/new/path/

Does anyone know how to solve this? I saw a couple of pages like How to edit a JSON file using shell? but they were covering either bigger problems, or trying to delete keys. Something that I don't need.


New contributor
Diego Alejandro Gómez Pardo is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1


Use backslashes to escape the double quotes:

jq ".out_directory = \"$new_out_directory\""

But it's safer to use a parameter so you don't have to worry about special characters in the variable value:

jq --arg d "$new_out_directory" '.out_directory = $d' 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .