LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-07-2011, 11:33 AM   #1
s_linux
Member
 
Registered: Jul 2009
Posts: 80

Rep: Reputation: 15
for loop question


I know we can do something like

Quote:
for i in 1 2 3 4
do
something
done
Can we also do something like below. I tried with below but it is giving me syntax violation. Could some one explain me why?


Quote:
for i in
1
2
3
4
5
do
something
done
Thanks
 
Old 09-07-2011, 12:33 PM   #2
instinct46
LQ Newbie
 
Registered: Dec 2005
Location: Liverpool
Distribution: Slackware
Posts: 18

Rep: Reputation: 0
Would you not be able to post the code??
 
Old 09-07-2011, 01:28 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
Originally Posted by s_linux View Post
I tried with below but it is giving me syntax violation. Could some one explain me why?
There is not a reason. It is the shell syntax that requires the list to be on the same line. The question is: why do you think it should work and what is the aim of a vertical list compared to the in-line one?
 
Old 09-07-2011, 01:52 PM   #4
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
You can escape the line feeds (line continuation) at the end of lines:

Code:
for color in \
  red        \
  white      \
  blue
do
  echo $color
done
Just make sure that the backslashes are the very last character on the line.
 
Old 09-07-2011, 06:26 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
You can always store your elements in a variable first. Word-breaking occurs after variable expansion, and newlines are considered whitespace delimiters.

Code:
list='1
2
3
4'

for i in $list; do
	something
done
Note that this is one of the few situations where you do not want to quote the variable.

Arrays are better when you have lists of elements to process, however.
Code:
list=( 1 2 3 4 )

for i in "${list[@]}"; do
	something
done

PS: Please use [code][/code] tags around your code, to preserve formatting and to improve readability. Don't use quote tags, which don't protect whitespace.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Loop Question SoulShaker Linux - Server 5 06-17-2009 01:44 PM
C programming Loop question? chibi2666 Programming 8 06-16-2009 09:15 PM
while loop question IceOner Programming 6 10-26-2007 08:23 AM
BASH 'while' loop question GSMD Programming 4 04-13-2007 12:51 AM
vbs loop question jonlake Programming 3 10-05-2006 05:32 PM


All times are GMT -5. The time now is 08:48 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration