Sample Header Ad - 728x90

Is there a syntax for a "for" loop over words or lines in a variable, that will work unmofdified in both bourne shell and zsh?

1 vote
2 answers
2224 views
I have a variable that contains a list of strings, one per line, to be looped over in a for...in...do...done command. I move regularly between bourne shell and zsh. As far as I can understand it, zsh doesnt by default split words out of a a string at newline or whitespace; bourne shell does. So commands like for list_item in $list; do... fail in zsh whereas they'd work in bourne shell, or with literal text rather than variable. Ive tried playing with IFS= in the command, and quoting the variable, but can't seem to make progress. Is there a single syntax for a loop over the words/lines in the string, that will work unmodified in both shells, for ease? if not, what's best practice?
Asked by Stilez (1311 rep)
Oct 14, 2023, 10:46 AM
Last activity: Oct 19, 2023, 02:04 PM