Assign dev, test, and prod to env in that order.
Run printf once for each value.
Which loop is appropriate?
for name in words; do commands; done assigns each word to name in order and runs the body for every element.
Detailed explanation
while env in dev test prod; do printf '%s\n' "$env"; doneIncorrect. while needs a condition command and does not iterate this list form.
if env in dev test prod; then printf '%s\n' "$env"; fiIncorrect. if performs one conditional branch, not iteration.
case env in dev test prod) printf '%s\n' "$env";; esacIncorrect. case selects a pattern for one value rather than assigning all three.
for env in dev test prod; do printf '%s\n' "$env"; doneCorrect. The for loop assigns each listed value to env in order.
Try it yourself
An example you can run in a temporary verification environment.
sh -c 'for env in dev test prod; do printf "%s\n" "$env"; done'Expected result
dev、test、prodを1行ずつ表示Key points
- for iterates a list
- Values follow in
- The body is between do and done
Notes
- Environment: POSIX sh / 状態変更なし
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Shell function
A reusable group of commands that runs in the caller's shell context.
Positional parameter
A numbered argument or special value supplied to a script or function.
Control flow
Conditions, loops, and exit statuses determine which shell commands run.