Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 20th, 2005, 3:28 AM   #1
rdktz
Newbie
 
Join Date: Jan 2005
Posts: 2
Rep Power: 0 rdktz is on a distinguished road
a bash puzzle

Hi guys,

I've been trying to investigate why these two scriptlets give different results:

1.

obcy:~ kor$ for file in "an example"; do echo $file; done
an example

2.

obcy:~ kor$ for file in `echo \"an example\"`; do echo "["$file"]"; done
["an]
[example"]

i'd be gratefull for any hints

Greetings,
Radek.
rdktz is offline   Reply With Quote
Old Jan 20th, 2005, 8:39 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
It seems that
echo \"an example\"
adds a \n character in the middle for some odd reason. I'm not sure why it's doing it. But since you print $file with [] on both sides, and those [] appear around both words in the second example. It makes me thing tha t there is a newline character added somewhere.

Diz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Jan 20th, 2005, 9:40 AM   #3
rdktz
Newbie
 
Join Date: Jan 2005
Posts: 2
Rep Power: 0 rdktz is on a distinguished road
Hi and thanks for your answer,

in fact it doesn't need to be a new line since list separators are, as far as I know, any blank characters which are not escaped. The question really is why the quotes are passed to the output without being interpreted by the outer shell (which explains line-break)

Greeting,
radek
rdktz is offline   Reply With Quote
Old Jan 20th, 2005, 9:59 AM   #4
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 5 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
well \" is the escape for " as is \\ the escape for \ etc etc etc so you are telling the compiler to ignroe the queotes as normal programming and include them as the string.
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC