Shell function variables not properly scoped in background jobs #2340
Unanswered
techbiz-sadakane-kazuma
asked this question in
Q&A
Replies: 1 comment
-
You could try to do this with the commands you want to run in the "background": Otherwise, interesting problem. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Shell function variables not properly scoped in background jobs
Description
When using shell functions with background jobs (
&
) in Taskfile, variables appear to be shared/overwritten across parallel executions, leading to incorrect values being processed.Expected Behavior
Each background job should receive and process its unique loop variable:
Actual Behavior
All background jobs process the same (final) value:
Minimal Reproduction Case
Taskfile.yml:
Run:
task test_parallel
Working Comparison
The same code works correctly when executed as a standalone shell script:
test.sh:
Output: Correctly shows "Processing 1", "Processing 2", etc.
Environment
Additional Context
This appears to be related to how Taskfile handles shell execution context and variable scoping in background processes. The workaround is to call the external script (
bash test.sh
), but it would be helpful to understand if this is expected behavior or if there's a way to make inline shell functions work correctly with background jobs.Question
Is this expected behavior due to how Taskfile executes shell commands, or is there a recommended way to achieve proper variable scoping for background jobs within Taskfile?
Beta Was this translation helpful? Give feedback.
All reactions