Skip to content

Instantly share code, notes, and snippets.

@ceremcem
Last active January 7, 2017 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceremcem/2f399afef92332bcd5d7b778a1da302d to your computer and use it in GitHub Desktop.
Save ceremcem/2f399afef92332bcd5d7b778a1da302d to your computer and use it in GitHub Desktop.
#!/bin/bash
# for question at http://superuser.com/questions/1069702/how-to-expand-a-loop-iterating-over-an-array-dynamically-in-bash/1159076#1159076
req="a b c d"
read -a MODULES <<<$req
i=0
for ((i = 0; i < "${#MODULES[@]}"; i++)); do
MODULE_NAME=${MODULES[$i]}
echo $MODULE_NAME
if [ i == 0 ]; then
MODULES+=( "e" )
i=1
fi
done
# should give following output:
#
# a
# b
# c
# d
# e
# but gives following output:
#
# a
# b
# c
# d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment