LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 02-16-2010, 12:15 AM   #1
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Rep: Reputation: 15
for loop error....


Conider the following code..

1 #!/bin/bash
2
3 for tst in I don't know if this'll work
4 do
5 echo "word :: $tst"
6 done

Why the o/p is so amgious ?

word :: I
word :: dont know if thisll
word :: work
 
Old 02-16-2010, 12:22 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,896

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
Quote:
for tst in I don't know if this'll work
The single quotes in don't and this'll constitute a single quoted phrase withing quotes which prevent the shell from parsing the phrase.

Try this instead:
for tst in I don\'t know if this\'ll work

using the backslash to escape the single quotes.

word :: I
word :: don't
word :: know
word :: if
word :: this'll
word :: work
 
Old 02-16-2010, 12:35 AM   #3
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Original Poster
Rep: Reputation: 15
I know .... but why the op is so illogical ??
 
Old 02-16-2010, 01:17 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Quote:
Use double quotes to prevent word splitting. [3] An argument enclosed in double quotes presents itself as a single word, even if it contains whitespace separators.
http://www.linuxtopia.org/online_boo...uotingvar.html

Just about all programming langs have an equivalent (often the same) rule to allow 'values' to have spaces or other (normally) special chars in them.
 
Old 02-16-2010, 06:03 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by lipun4u View Post
I know .... but why the op is so illogical ??
Actually it's quite logical. The shell interpret the string
Code:
I don't know if this'll work
as
Code:
I dont know if thisll work
where the part in red is not split into fields (due to the single quotes, as already mentioned). The string concatenation do the rest.

The blank spaces that actually act as field separators are shown here:
Code:
I dont know if thisll work
 ^                   ^
 
  


Reply


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
Error about loop device... Kindly help.. ashishshukla Linux - Server 1 05-30-2009 06:16 PM
segmentation error while using mpz_powm in a loop shrims4u Linux - Software 0 05-07-2009 12:23 PM
Strange error using grep in a while loop anil3 Linux - General 4 03-30-2009 04:09 AM
error: ‘for’ loop initial declaration nasim751 Programming 3 06-07-2008 11:31 AM
error: ‘for’ loop initial declaration nasim751 Linux - Software 2 06-07-2008 06:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:27 AM.

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