does linux shell support list data structure?
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
Comments
-
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 almost 12 yearsTo iterate over the array, use
for item in ${colors[*]}; do echo $item; done
-
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 almost 12 yearswhy "light gray" will be treated as two items when * is used? where i can find documents for such details? man sh?
-
tripleee almost 12 yearsBasically 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 over 8 yearsWhat does the @ mean??
-
chepner over 8 yearsIt's a special index that causes the expansion to produce all elements of the array, not just one specific element.
-
Svech87 over 7 yearsI didn't get why
for i in "${array[@]}";
prints 3 lines. Sinceecho ${array[@]}
prints a single lineitem 1 item 2 item 3
, I would expect thatfor i in "${array[@]}";
is converted infor i in "item 1 item 2 item 3";
; so it would result in a single lineitem 1 item 2 item 3
. What am I missing? Thanks in advance -
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 about 6 yearsarray only works in bash shell. Is there a way to achieve same in KSH?
-
chepner about 6 yearsWhat 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.