bash loop through list of strings
130,126
Solution 1
Using arrays in bash can aid readability: this array syntax allows arbitrary whitespace between words.
strings=(
string1
string2
"string with spaces"
stringN
)
for i in "${strings[@]}"; do
echo "$i"
done
Solution 2
You can escape the linebreak with a backslash:
$ for i in \
> hello \
> world
> do
> echo $i
> done
hello
world
$
Solution 3
You may escape the newlines before/after each item that you loop over:
for i in \
string1 \
string2 \
stringN
do
printf '%s\n' "$i"
done
Or, for this simple example:
printf '%s\n' string1 string2 stringN
which has the same result.
Related:
Variation using a bash
array:
strings=(
string1
string2
stringN
)
printf '%s\n' "${strings[@]}"
Solution 4
list='a b c d'
for element in $list;do
echo "$element"
done
Solution 5
If switching to zsh
is an option:
for string (
string1
'other string'
etc..
) printf '%s\n' "$string"
Related videos on Youtube

Author by
Admin
Updated on September 18, 2022Comments
-
Admin 3 months
Is it possible to format this sample:
for i in string1 string2 stringN do echo $i done
to something similar to this:
for i in string1 string2 stringN do echo $i done
EDIT: Sorry for confusion, didn't realize that there was different methods of executing script - sh
<scriptname>
versusbash <scriptname>
and also this thing which I cannot name right now -#!/bin/sh
and#!/bin/bash
:)-
jesse_b over 4 yearsWhat is the problem you are trying to solve?
-
Pankaj Goyal over 4 yearsDid you try what you're asking to see whether it'd work?
-
Admin over 4 years@Jesse_b readability and managebility of bunch of strings
-
Admin over 4 years@DopeGhoti yes I did
-
jesse_b over 4 years@waayee: In that case an array is your best bet. See Glenn Jackman's answer.
-
-
Admin over 4 yearsThis seems most elegant, but unfortunately gives error: Syntax error: "(" unexpected
-
ilkkachu over 4 years@waayee, then you're not running it in Bash. Remember that
sh
isn't necessarily Bash, and especially isn't on Debian and Ubuntu. -
Admin over 4 years@ilkkachu Think I get it now - must run "bash <scriptname>" not "sh <scriptname>" :)
-
rrrrr about 4 years@glennjackman: what does the
@
do? If I wanted "${strings[@]}" to be separated by commas instead of spaces, is it easy to do that? -
Angel Todorov about 4 years@rrrrr, your questions are probably answered here: stackoverflow.com/q/12314451/7552
-
Dims over 3 yearsSyntax error "( unexpected"
-
Angel Todorov over 3 yearsRead the other comments - that error has already been discussed.
-
41754 over 3 yearsYou are missing semicolons!
-
Kusalananda over 3 yearsNote that this would work as long as each string is a separate word. As soon as you have spaces in your strings, the spaces would break the string apart into multiple words. Also, if the
$list
string contains filename globbing characters (list='* * * *'
), the shell would potentially replace these with matching filenames. -
Stephen Kitt almost 3 years
string1
etc. aren’t the literal values to be displayed, they’re placeholders, so this approach doesn’t work.