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"
Share:
130,126

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    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> versus bash <scriptname> and also this thing which I cannot name right now - #!/bin/sh and #!/bin/bash :)

    • jesse_b
      jesse_b over 4 years
      What is the problem you are trying to solve?
    • Pankaj Goyal over 4 years
      Did you try what you're asking to see whether it'd work?
    • Admin
      Admin over 4 years
      @Jesse_b readability and managebility of bunch of strings
    • Admin
      Admin over 4 years
      @DopeGhoti yes I did
    • jesse_b
      jesse_b over 4 years
      @waayee: In that case an array is your best bet. See Glenn Jackman's answer.
  • Admin
    Admin over 4 years
    This seems most elegant, but unfortunately gives error: Syntax error: "(" unexpected
  • ilkkachu
    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
    Admin over 4 years
    @ilkkachu Think I get it now - must run "bash <scriptname>" not "sh <scriptname>" :)
  • rrrrr
    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
    Angel Todorov about 4 years
    @rrrrr, your questions are probably answered here: stackoverflow.com/q/12314451/7552
  • Dims
    Dims over 3 years
    Syntax error "( unexpected"
  • Angel Todorov
    Angel Todorov over 3 years
    Read the other comments - that error has already been discussed.
  • 41754
    41754 over 3 years
    You are missing semicolons!
  • Kusalananda
    Kusalananda over 3 years
    Note 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
    Stephen Kitt almost 3 years
    string1 etc. aren’t the literal values to be displayed, they’re placeholders, so this approach doesn’t work.