problem with for loop and a variable with spaces...Hi


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with for loop and a variable with spaces...Hi
# 1  
Old 04-20-2010
Question problem with for loop and a variable with spaces...Hi

Hi there,

I don't understand the following behavior:
Code:
toto:~$ for word in un "deux trois"; do echo $word; done
un
deux trois
toto:~$ sentence='un "deux trois"'
toto:~$ for word in $sentence; do echo $word; done
un
"deux
trois"
toto:~$ sentence="un 'deux trois'"
toto:~$ for word in $sentence; do echo $word; done
un
'deux
trois'

Why is the word with a space split in two parts even though it is surrounded by quotes in the variable $sentence?
Is there any way to get the same behavior with a variable and with an explicit string?

Thanks for your help.
Santiago
# 2  
Old 04-20-2010
Hi.

Unless you quote $sentence itself in your for loop, it's still just a collection of words each individually assigned to $word.

Code:
$ sentence="un 'deux trois'"
$ for word in "$sentence"; do echo "$word"; done
un 'deux trois'

# 3  
Old 04-20-2010
... and if you quote $sentence, there's no point in using a loop, since without field splitting there will always be just one word after the parameter expansion Smilie

Quote characters stored in a variable are not special in any way. The variable's expansion occurs after the shell has identified quoted strings.
# 4  
Old 04-20-2010
Quote:
Originally Posted by alister
... and if you quote $sentence, there's no point in using a loop, since without field splitting there will always be just one word after the parameter expansion Smilie
Of course Smilie But I suppose if there were more than one "sentence" it would hold water!
# 5  
Old 04-21-2010
Hi all and thanks for your cooperation.
I think I didn't express myself correctly.

What I'm expecting is to have the sentence un "deux trois" split in two words: un and deux trois.

It's easy to achieve using an explicit string. But is there any way I can do that with the sentence in a variable?

Thanks
Santiago
# 6  
Old 04-21-2010
Code:
$ eval "for word in $sentence; do echo \$word; done"
un
deux trois

As always, eval should not be used on untrusted, unsanitized input.

Regards,
Alister
# 7  
Old 04-21-2010
Some shells support arrays.

For example zsh, bash and ksh93 support this syntax:

Code:
sentence=(un "deux trois")
for w in "${sentence[@]}"; do
  printf '%s\n' "$w"
done

With the Bourne shell, you can use something like this (you'll loose any previously set positional parameters):

Code:
set -- un "deux trois"
for w in "$@"; do
  printf '%s\n' "$w"
done

Some shells support the following shortcut in this context:

Code:
set -- un "deux trois"
for w; do
  printf '%s\n' "$w"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

String variable concatenation through loop problem

Hi Team!! Please can anyone tell me why the following line does not work properly? str3+=$str2 it seems that str3 variable does not keep its value in order to be concatenated in the next iteration! Thus when i print the result of the line above it returns the str2 value What i want to do is to... (8 Replies)
Discussion started by: paladinaeon
8 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. Shell Programming and Scripting

Loop with Find—damn spaces!

Hi Guys, I'm trying to find all files with a particular extension and then loop some actions. The problem is that if the files have spaces in their names I get end up being each word as a separate result rather than the entire file. ext=".txt" out=".rtf" for i in $( find "$1" -name "*$ext" );... (9 Replies)
Discussion started by: imonkey
9 Replies

5. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

6. Shell Programming and Scripting

for loop ( string having spaces )

Dear All, i facing problem to use string having spaces in for loop.. file used for FOR LOOP command.txt rpm -t -v ttm -D -r RJLL -h YELP rpm -t -v ttm -D -r RJLL -h ERRT rpm -t -v ttm -D -r RJLL -h TYYE rpm -t -v ttm -D -r RJLL -h POOL CODE using for execute above command... (3 Replies)
Discussion started by: arvindng
3 Replies

7. UNIX for Dummies Questions & Answers

For loop using find with file name spaces

Hello All, This question is actually for the service console of VMware ESX 3.5 but is relevant to this forum I think. I have been advised to use the following commands: for i in `find /vmfs/volumes/Test_VMFS/ -name "*.vmx"` do echo "$i" #sed -i 's/scsi1:0.present =... (3 Replies)
Discussion started by: mronsman
3 Replies

8. Shell Programming and Scripting

Variable problem in for loop with if statement

Hi, Again a little problem. Do not understand good why an empty string is not detected. Here is the program: #!/bin/ksh APR=`date | grep Apr | awk '{print $2$3}'` MAY=`date | grep May | awk '{print $2$3}'` JUN=`date | grep Jun | awk '{print $2$3}'` echo "Variable Apr has value:... (6 Replies)
Discussion started by: ejdv
6 Replies

9. Shell Programming and Scripting

Problem with variable expension using for loop.

Hi, I have a problem with expending variables when used in a for loop: #!/bin/ksh VAR1=aaa VAR2=bbb VAR3=ccc for ITEM in VAR1 VAR2 VAR3 do echo "${ITEM}" done This gives: VAR1 VAR2 VAR3 (2 Replies)
Discussion started by: ejdv
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question