Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass bash variable in yq

Tags:

bash

yq

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/

but I am getting this error(Actual output)

Error: Value for env variable '$variable1' not provided in env()

Please note: I am trying to fetch corporate-url value, using a bash variable, constraint is that I cannot pass string directly in yq as the value of temp changes as this snippet is running inside a for loop which changes value of temp every time so cannot hard code for a particular value.

Reference YQ Documentation: https://mikefarah.gitbook.io/yq/operators/env-variable-operators

ApisDraft folder contains multiple yml files

ApisDraft=$(find drafts/* -maxdepth 1 -type f)
for ApiFixOrgsTags in $ApisDraft
do
  my_var=$(yq '.securityDefinitions.[].tokenUrl' $ApiFixOrgsTags)
  ConfigProper='.configuration.Properties.'
  CatelogProper='.configuration.catalogs.[].Properties.'
  variable1=$ConfigProper${my_var}
  variable2=$CatelogProper${my_var}
  # to remove white all spaces
  variable1= echo $variable1 | sed -E 's/(\.) */\1/g'
  variable2= echo $variable2 | sed -E 's/(\.) */\1/g'
  export $variable1
  export $variable2
  Value1=$(yq "$variable1" $ApiFixOrgsTags)
  Value2=$(yq '.[env($variable2)]' $ApiFixOrgsTags)
done
like image 540
Dhaval Patel Avatar asked Nov 16 '25 12:11

Dhaval Patel


1 Answers

In this case, you don't need to put it in the environment. Let the shell expand it so yq just sees the value of the variable:

yq "$temp" test.yml     # => https://stackoverflow.com/
like image 105
glenn jackman Avatar answered Nov 18 '25 12:11

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!