You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to use env and vars in the same task definition #843
---
# https://taskfile.devversion: '3'tasks:
foobar:
desc: Use both vars and env in a single task.cmds:
- echo {{.FOO}} {{.BAR}}vars:
FOO: Fizzenv:
BAR: Buzz
...
Output
When using the YAML file above, I get the following
> task foobar
task: [foobar] echo Fizz
Fizz
The second part of this is missing. However, if I place the env section at the root level of the YAML file, it works as expected.
seems like a bug regarding {{.BAR}}, when environment variables are defined in env block at the cmd level, they can not use templating but if you do export BAR=bar you can use with {{.BAR}} or if you define your env variable at the top level of the file, you can use {{.BAR}}
@filbotblueenv variables are made available to your shell rather than the templating engine, so to echo the value, you would do echo {{.FOO}} $BAR instead of echo {{.FOO}} {{.BAR}} as per the documentation.
Looking back through previous versions, outputting task-level env variables via the templating engine has never worked and is not documented, so I wouldn't consider this a bug. In fact, I'm not sure if the ability to use globally defined env variables in the templating engine is intended behaviour. I'll admit, the inconsistency is definitely slightly confusing.
@andreynering do you know if accessing globally defined env variables using the templating engine is intended/supported? If not, should we add a warning when using this feature? (since removing it would not be backward-compatible).
In theory this is a bug, but I would discourage the use of env for this propose. Why not use vars? env should be used to make environment available to the commands you're running.
Globally defined env is made available to templating engine, effectively as vars, which is a well known source of confusion. Task defined env is 'injected' into the environment of the task commands. In this case, that is confused with the operation of vars. The correct command would be:
- echo {{.FOO}} $BAR
The reason this exists is because some commands can only be configured/operated by environment variables. In other cases, vars and the templating system is typically less confusing to use.
Activity
occamist commentedon Sep 1, 2022
seems like a bug regarding {{.BAR}}, when environment variables are defined in env block at the cmd level, they can not use templating but if you do
export BAR=baryou can use with {{.BAR}} or if you define your env variable at the top level of the file, you can use {{.BAR}}pd93 commentedon Sep 10, 2022
@filbotblue
envvariables are made available to your shell rather than the templating engine, so to echo the value, you would doecho {{.FOO}} $BARinstead ofecho {{.FOO}} {{.BAR}}as per the documentation.Looking back through previous versions, outputting task-level
envvariables via the templating engine has never worked and is not documented, so I wouldn't consider this a bug. In fact, I'm not sure if the ability to use globally definedenvvariables in the templating engine is intended behaviour. I'll admit, the inconsistency is definitely slightly confusing.@andreynering do you know if accessing globally defined
envvariables using the templating engine is intended/supported? If not, should we add a warning when using this feature? (since removing it would not be backward-compatible).andreynering commentedon Sep 12, 2022
In theory this is a bug, but I would discourage the use of
envfor this propose. Why not usevars?envshould be used to make environment available to the commands you're running.trulede commentedon Dec 5, 2025
Globally defined env is made available to templating engine, effectively as vars, which is a well known source of confusion. Task defined env is 'injected' into the environment of the task commands. In this case, that is confused with the operation of vars. The correct command would be:
- echo {{.FOO}} $BARThe reason this exists is because some commands can only be configured/operated by environment variables. In other cases, vars and the templating system is typically less confusing to use.