0

I have several files that I need to update accross several directories. Normally I would do that with a bash loop. I was trying to do the same with YQ, but with no luck.

Just to point out, since the question's been flagged as a duplicate of "Bash: difference between single and double quotes" - this is not a question about bash syntax, but yq syntax.

I was hoping this would work:

for i in one two three four;
do
yq -i '.key = "$i-something"' ./$i/file.yaml;
done

This would, however, store the literal value of what's been given, so it would be key: $i-something, which is not what I need. I would need it to be like key: one-something.

If I tried switching the quotes, like yq -i ".key = '$i-something'" ./$i/file.yaml; that would produce an error stating Error: 1:12: invalid input text "'one..."

Any ideas?

  • You could swap the quotes, double quotes are the _outside quotes_ and single quotes are the _inner quotes_ – Jetchisel Feb 02 '23 at 12:19
  • That gives this error: Error: 1:12: invalid input text "'one-..." – Богдан Божић Feb 02 '23 at 12:21
  • You might want to tell which version of yq you're using. Apparently there's a wrapper version for jq and https://github.com/mikefarah/yq. – konsolebox Feb 02 '23 at 13:27
  • 1
    What exactly was your problem with @Kamuffel's deleted answer? Passing the value of `$i-something` via `--arg` is preferred to trying to dynamically generate a filter that contains the value. – chepner Feb 02 '23 at 13:35
  • I can't find the command in the history so I cannot reproduce and tell exactly, and I don't remember what exactly the syntax was like, something like -i --arg var that was used in substitution, but yq that I use doesn't accept that as an option. – Богдан Божић Feb 02 '23 at 21:06

1 Answers1

2

You need to use double quotes in both places:

for i in one two three four; do
  yq -i ".key = \"$i-something\"" ./$i/file.yaml;
done
larsks
  • 277,717
  • 41
  • 399
  • 399
  • quoting mess makes me headaches..... did you try groovy with bash???? (Jenkinsfile) that's the winner so far for creating headaches... – U.V. Jun 20 '23 at 21:58