For loop respects quotes but not in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop respects quotes but not in a variable
# 1  
Old 07-23-2014
For loop respects quotes but not in a variable

Behavior I am looking for:
Code:
$ for var in a 'b c'; do echo $var; done
a
b c

What I am getting when I try to use a variable:
Code:
$ test="a 'b c'"
$ for var in $test; do echo $var; done
a
'b
c'

Is there a workaround?

Mike
# 2  
Old 07-23-2014
How about this:

Code:
$ test=(a 'b c')
$ for var in "${test[@]}"; do echo $var; done
a
b c

or

Code:
$ set a 'b c'
$ while [ $# -gt 0 ]; do echo $1; shift ; done
a
b c

# 3  
Old 07-23-2014
This does not get around my problem of having variable input.
Code:
 
test=($some_some_variable_with_quotes)

breaks it.

Mike

---------- Post updated at 02:14 PM ---------- Previous update was at 02:02 PM ----------

I'm currently working on a workaround that uses \n

Whatever code is parsing arguments in bash works the way I want this to work

Code:
 
script Argument1 "Argument 2" 'Argument 3'

sets $1 $2 and $3 just as you would expect/want

I am trying to parse commands from STDIN or a file the same way.

Mike
# 4  
Old 07-23-2014
I guess here you are trying to avoid using eval:

Code:
eval test=($some_some_variable_with_quotes)

# 5  
Old 07-23-2014
Shell doesn't do that kind of doublethink. If something's in a variable, it's taken literally (with the exception of glob characters).

Just like in your other thread:

If you explain your actual goal -- not the means you've picked to accomplish it -- we could help you far better!

Your questions suggest you've taken a wrong turn and ended up building a shell-inside-the-shell kind of thing, instead of just using the shell's features in the first place (i.e. building a configuration-file-reader in the shell, instead of just sourcing the file). Not knowing what the shell can do often leads to this kind of catch-22 situation.

But for the record, the xargs utility can understand quotes and print properly grouped lines:

Code:
$ xargs -n 1 <<EOF
a b c d e
"1 2 3 4 5"
'q asdf ads f'
EOF

a
b
c
d
e
1 2 3 4 5
q asdf ads f

$


Last edited by Corona688; 07-23-2014 at 06:31 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 6  
Old 07-23-2014
I thought I did just that above. I'll try again:

I have a file with argments in it. There can be multiple arguments per line. Arguments can contain whitespace (in which case they are quoted). I want to itterate through them and handle them (such as with a case statement). I want the itteration to separate on unqoted white space but not quoted whitespace.

Mike

PS. Will check out the evel suggestion.
PPS. Will check out the xargs suggestion too.
# 7  
Old 07-23-2014
Never use eval if you can possibly avoid it. If your file contains `rm -Rf ~/` and you feed that text into eval, eval will execute that code!

You explained one tiny corner of one tiny corner of your problem. What exactly is this line-by-line loader thing for? Is it a part of anything larger?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Spaces in double quotes in variable ?

Hi got this issue and was wondering if someone could please help out ? var='." "' echo $var ." " I 'll get ." " and not ." with 10 spaces in between " Thanks (3 Replies)
Discussion started by: stinkefisch
3 Replies

2. Shell Programming and Scripting

Expansion of variable inside Single Quotes Fails!!

I am unable to expand the value of entry variable inside the nawk command. I tried three different nawk command as below but none of them substitute the value of entry variable. ls *.txt | while IFS='' read -r entry; do #nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"$entry)}... (9 Replies)
Discussion started by: mohtashims
9 Replies

3. Shell Programming and Scripting

How to pass two words within double quotes as variable?

Hi All, OS - Suse 10 ksh --version version sh (AT&T Research) 93s+ 2008-01-31 I am passing two words within double quotes ("Application Developer") to script as variable, but script is adding two single quotes between two words like ("Application' 'Developer"). below is simple test... (4 Replies)
Discussion started by: srimitta
4 Replies

4. UNIX for Dummies Questions & Answers

How to grep exact string with quotes and variable?

As the title says I'm running a korn script in attempts to find an exact match in named.conf finddomain.ksh #!/bin/ksh # echo "********** named.conf ************" file=/var/named/named.conf for domain in `cat $1` do grep -n '"\$domain "' $file done echo "********** thezah.inc... (1 Reply)
Discussion started by: djzah
1 Replies

5. Shell Programming and Scripting

Double quotes and variable proble in echo

HI Guys, I want to echo below line in my file :- lpd | grep AL | nawk '{print "1dLA - " $0} How can i echo same Output (4 Replies)
Discussion started by: pareshkp
4 Replies

6. Shell Programming and Scripting

Interpolate a variable with single quotes

I need to interpolate a shell variable in a code, i cannot share the exact code so this is an example i made up to describe the situation What I am trying to do here is try to wrap up the value of a variable in single quotes. This value needs to be passed to another program which would only... (4 Replies)
Discussion started by: sam05121988
4 Replies

7. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

8. Shell Programming and Scripting

sed replace spaces between quotes with a variable

I have lines with: elseif (req.http.host ~ "^(www.)?edificationtube.com$|www.edificationtube.org www.edificationtube.net edificationtube.org www.edificationtube.com edificationtube.net") { elseif (req.http.host ~ "^(www.)?collegecontender.com$|www.collegecontender.com collegecontenders.com... (3 Replies)
Discussion started by: EXT3FSCK
3 Replies

9. Shell Programming and Scripting

Using echo to print double quotes along with variable substitution

Hi, I am generating html code using cshell, but i am having one problem while printing double quotes, I need to write following code in file. where $var contains list of web address <a href="$var">$var</a> So i am using echo "<a href="$var">$var</a>" > file.html But with this " in... (4 Replies)
Discussion started by: sarbjit
4 Replies

10. Shell Programming and Scripting

Double Quotes within a variable

This is a totally dumb newbie question, but I have not been able to find t he answer in the BASH book or online. I am trying pass a double quoted variable to the command line. variable = "-b \"dc=example,dc=com\"" When I run sh -x the variable comes out as '-b "dc=example,dc=com"' is... (4 Replies)
Discussion started by: burton_1080
4 Replies
Login or Register to Ask a Question