Sample Header Ad - 728x90

yq to replace YAML property with multiple words value

0 votes
2 answers
149 views
Given a YAML file, example.yaml:
services:
  api:
    image: myImage
    ports:
      - 8080:8080
    environment: 
      - "TEST_STRING_WITH_SPACE=?"
I need to replace the TEST_STRING_WITH_SPACE environment variable value with a multiple words string param. Expected is like that:
services:
  api:
    image: myImage
    ports:
      - 8080:8080
    environment: 
      - "TEST_STRING_WITH_SPACE=With Space"
I'm using yq tool, version: yq (https://github.com/mikefarah/yq/) version v4.44.5 When I try to set value with a string without space, it works: yq '.services.api.environment = "TEST_STRING_WITH_SPACE='$(echo "WithoutSpace")'"' example.yaml But, when I try to set value with a string with space, it does't works: yq '.services.api.environment = "TEST_STRING_WITH_SPACE='$(echo "With Space")'"' example.yaml Got the error: > Error: 1:32: invalid input text "\"TEST_STRING_WIT..." How fix it?
Asked by Allan Andrade (133 rep)
Mar 20, 2025, 01:14 PM
Last activity: Apr 9, 2025, 11:27 AM