Search code examples
windowsdockergogo-templatesdocker-command

template parsing error: template: :1: unexpected "=" in operand


template parsing error: template: :1: unexpected "=" in operand

I got the above error when executing the below command in Windows,

docker inspect --format="{{range $key, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "VERSION"}}{{$value}}{{end}}{{end}}" octopusbi-agent-backend

What could be the issue?


Solution

  • The issue with the "=" symbol, if you use a double quotation(") mark inside the string enclosed with the double quotation(") marks, you have to add a backslash(\) before every double quotation(") mark excluding the first and last double quotation(") mark.

    example:-

    "Hello "your_name""  <-- wrong
    "Hello \"your_name\""  <-- correct
    

    Windows

    As I mentioned earlier I changed "=" to \"=\" and after that, I got another issue related to some other string value called "VERSION". For that also I had to change "VERSION" to \"VERSION\" and it worked as I expected.

    enter image description here

    So the final command is,

    docker inspect --format="{{range $key, $value := .Config.Env}}{{if eq (index (split $value \"=\") 0) \"VERSION\"}}{{$value}}{{end}}{{end}}" octopusbi-agent-backend
    

    Ubuntu

    I ran the same command in Ubuntu with the starting and ending quotations with single quotation(') marks and kept the rest of the double quotation(") marks.

    enter image description here

    So the final command is,

    docker inspect --format='{{range $key, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "VERSION"}}{{$value}}{{end}}{{end}}' octopusbi-agent-backend
    

    Summary

    If you use the docker inspect command with the --format option,

    • In Windows:-
      1. You have to start the format string with double quotation(") marks.
      2. If you want to use a double quotation(") mark inside the format string, use \" instead.
    • In Ubuntu:-
      1. You have to start the format string with single quotation(') marks.
      2. Feel free to use double quotation(") marks inside the format string.

    In the shortest, we have to use the double quotation(") mark inside the format string for both environments if you need to use the quotation mark.