Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-31-2012, 11:24 PM   #1
LQ Newbie
Registered: Sep 2010
Location: bangalore
Posts: 5

Rep: Reputation: 0
Smile How to read properties valuues in for loop..?

Hi guys,

Can someone help me for clearing below doubt please.Thanks in advance

"" file contains

"" test script file contain

for value in $TEST_VALUES
echo Value:$value

Here , I want output as

But I am getting output as
Old 03-31-2012, 11:38 PM   #2
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Can be changed? The "for var in $array" syntax separates entries by spaces. Since your entries are separated by commas, you have three choices:

1) Modify to look like TEST_VALUES="A B C D"
2) Modify your shell to separate by commas instead of (or in addition to) spaces
3) Use a different method of parsing the entries in TEST_VALUES
Old 04-01-2012, 01:17 AM   #3
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The behaviour is expected. There is no reason why for value in $TEST_VALUES bash should split the value of TEST_VALUES at each comma. If it did then any variable that happens to have commas in its value would be split!

This works, using shell parameter expansion to split $TEST_VALUES at the commas as desired. It will break if the strings between commas include spaces. That could be fixed by creating an array out of $TEST_VALUES but would be more complex.


for value in ${TEST_VALUES//,/ }
    echo Value:$value
Note: I have presumed that you did not want to drop the "D" value as stated in the OP.
Old 04-01-2012, 12:05 PM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.


for value in $TEST_VALUES ; do
	echo "Value:$value"
2) Use an array instead.


for value in "${TEST_VALUES[@]}" ; do
	echo "Value:$value"
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.



#IFS=','			#alternately, using IFS

for value in "${TEST_VALUES[@]}" ; do
	echo "Value:$value"
See here for more string manipulation techniques:]string manipulation


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.

( 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.

for var in fee fai foo fum ; do

	if [[ "$var" == "foo" ]]; then
		echo "Found 'foo'."


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.
Old 04-10-2012, 02:27 AM   #5
LQ Newbie
Registered: Sep 2010
Location: bangalore
Posts: 5

Original Poster
Rep: Reputation: 0

@suicidaleggroll,@catkin,@David the H

Thaks for your valuable knoledge sharing regarding shell string manipulation.

Thanks & Regards
Rohith Sharma K S


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 09:47 PM
[SOLVED] Trying to read a file with a while loop. sinderone Programming 5 04-26-2010 03:37 AM
java: SQL query falsely read from properties file kpachopoulos Programming 0 01-08-2008 02:38 PM
while READ loop problem caps_phisto Linux - General 3 02-05-2007 10:58 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration