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.
Incorrect. 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.
Incorrect. 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.
Incorrect. 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.
Correct. 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
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.