does linux shell support list data structure?

189,554

Solution 1

It supports lists, but not as a separate data structure (ignoring arrays for the moment).

The for loop iterates over a list (in the generic sense) of white-space separated values, regardless of how that list is created, whether literally:

for i in 1 2 3; do
    echo "$i"
done

or via parameter expansion:

listVar="1 2 3"
for i in $listVar; do
    echo "$i"
done

or command substitution:

for i in $(echo 1; echo 2; echo 3); do
    echo "$i"
done

An array is just a special parameter which can contain a more structured list of value, where each element can itself contain whitespace. Compare the difference:

array=("item 1" "item 2" "item 3")
for i in "${array[@]}"; do   # The quotes are necessary here
    echo "$i"
done

list='"item 1" "item 2" "item 3"'
for i in $list; do
    echo $i
done
for i in "$list"; do
    echo $i
done
for i in ${array[@]}; do
    echo $i
done

Solution 2

For make a list, simply do that

colors=(red orange white "light gray")

Technically is an array, but - of course - it has all list features.
Even python list are implemented with array

Share:
189,554
hugemeow
Author by

hugemeow

http://serverfault.com/search

Updated on July 05, 2022

Comments

  • hugemeow
    hugemeow almost 2 years

    this question is not the same as Does the shell support sets?

    i know lots of script language support list structure, such as python, python, ruby, and javascript, so what about linux shell?

    does shell support such syntax?

    for i in list:
    do
         print i
    done
    

    i would first to initialize a list, for example:

    ListName = [ item1, item2, ..., itemn ]
    

    then iterate over it

    is that possible when programming shell scripts?

  • neevek
    neevek almost 12 years
    To iterate over the array, use for item in ${colors[*]}; do echo $item; done
  • chepner
    chepner almost 12 years
    @Neevek that won't do what you think it does; "light gray" will be treated as two items "light" and "gray". You need to use "${colors[@]}" (@ not *, and quoted).
  • hugemeow
    hugemeow almost 12 years
    why "light gray" will be treated as two items when * is used? where i can find documents for such details? man sh?
  • tripleee
    tripleee almost 12 years
    Basically that's a historical flaw, $* in the Bourne shell didn't work correctly in all situations and so "$@" had to be invented.
  • Vini.g.fer
    Vini.g.fer over 8 years
    What does the @ mean??
  • chepner
    chepner over 8 years
    It's a special index that causes the expansion to produce all elements of the array, not just one specific element.
  • Svech87
    Svech87 over 7 years
    I didn't get why for i in "${array[@]}"; prints 3 lines. Since echo ${array[@]} prints a single line item 1 item 2 item 3 , I would expect that for i in "${array[@]}"; is converted in for i in "item 1 item 2 item 3"; ; so it would result in a single line item 1 item 2 item 3. What am I missing? Thanks in advance
  • chepner
    chepner over 7 years
    "${array[*]}" would expand to "item 1 item 2 item 3". "${array[@]}" expands to "item 1" "item 2" "item 3"; it treats each element as a separate quoted word, rather than a single quoted word containing every element.
  • Mohammad Azim
    Mohammad Azim about 6 years
    array only works in bash shell. Is there a way to achieve same in KSH?
  • chepner
    chepner about 6 years
    What makes you think ksh doesn't support arrays? It does, but the syntax is a little different in places. This answer and its comments isn't the place to explain the differences.