Please use
[code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Yes, the problem here is due to word splitting. Since there are no spaces in the string, there's no splitting.
Two more options for you to consider.
1) Change the IFS value to a comma, so that it splits on them.
Code:
TEST_VALUES=A,B,C,D
IFS=','
for value in $TEST_VALUES ; do
echo "Value:$value"
done
2) Use an
array instead.
Code:
TEST_VALUES=( A B C D )
for value in "${TEST_VALUES[@]}" ; do
echo "Value:$value"
done
I personally recommend #2, if possible. Arrays are
designed for storing lists of values. If necessary, you can use IFS or catkin's technique above to split a scalar variable into an array.
Code:
TEST_VALUES=A,B,C,D
TEST_VALUES=( ${TEST_VALUES//,/ } )
#IFS=',' #alternately, using IFS
#TEST_VALUES=( $TEST_VALUES )
for value in "${TEST_VALUES[@]}" ; do
echo "Value:$value"
done
See here for more string manipulation techniques:
http://mywiki.wooledge.org/BashFAQ/100]string manipulation
Finally:
QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
( The above examples actually demonstrate one of the exceptions, but you should definitely quote the "echo" output variable. )
Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).
Many people also think that it's more readable to place the "
do/then" keywords on the same line as the "
for/while/until/if" keywords, as it more clearly separates the outside block from the inside block.
Code:
for var in fee fai foo fum ; do
if [[ "$var" == "foo" ]]; then
echo "Found 'foo'."
fi
done
Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.