LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training 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 03-25-2010, 10:07 PM   #1
minsi
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
how to use foreach in bash shell


Hi,

I used c-shell previously in unix. One of the useful command I used frequently is foreach.

> foreach a (`cat list`)
>> echo $a

I need to use bash shell now instead, and realized that I can't use foreach anymore. The command is not found.

Does anyone knows if there is similar command / function in bash shell?

Thanks for your help.
 
Old 03-25-2010, 10:16 PM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
Code:
for a in $(cat list)
do
  echo $a
done
 
Old 03-25-2010, 10:21 PM   #3
minsi
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for your reply, Ian

This is not something we can do on command line right? I'll need to put it in a bash script to use it?

For c-shell, I can use foreach in the command line itself.
 
Old 03-25-2010, 10:22 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Here's another way:

Code:
cat file | while read $line; do
  echo "$line"
done
or depending on exact context, this will work instead (I've yet to deliberately determine whether the above method or the below method is any better in any particular context):

Code:
while read line; do
  echo "$line"
done < file
Sasha

EDIT: And yes, you can do any of the aforementioned codes from blacky or myself, on the commandline.

Last edited by GrapefruiTgirl; 03-25-2010 at 10:23 PM.
 
Old 03-25-2010, 10:22 PM   #5
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
No, you can use this on the command line as well. - that applies to both answers
 
Old 03-25-2010, 10:31 PM   #6
minsi
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks a lot for all your help. It works now
 
Old 03-25-2010, 10:31 PM   #7
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 572

Rep: Reputation: 56
There are times when you need to use the second approach. If "cat file" produces space separated values for each line/record of the file (e.g. several words in a sentence), then the first "for/do/done" approach that I suggested will receive each "word" as a unique value of $a. If you want each "line" of the file to be processed in entirety, then my suggestion is not the answer for you and you should use the while/read approach.
 
Old 03-26-2010, 12:52 AM   #8
minsi
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the extra info. I'll keep that in mind
 
Old 03-26-2010, 01:32 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by GrapefruiTgirl View Post
Here's another way:

Code:
cat file | while read $line; do
  echo "$line"
done
or depending on exact context, this will work instead (I've yet to deliberately determine whether the above method or the below method is any better in any particular context):

Code:
while read line; do
  echo "$line"
done < file
Hello Sasha

The great advantage of the second method is that any variables set in the loop will still be set when the loop finishes. Full explanation here.
 
1 members found this post helpful.
Old 03-26-2010, 01:34 AM   #10
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Catkin -- thank you very much for that! Exactly what I had been wondering, and precisely the issue I have experienced on occasion: variables acting weirdly "local" to the loop (I think we chatted about this elsewhere at one point?)

Thanks!!

And: EDIT: http://heirloom.sourceforge.net/sh/sh.1.html#20 << for the non-bash shell equivalent (a bit more convoluted)

Sasha

Last edited by GrapefruiTgirl; 03-26-2010 at 01:36 AM. Reason: typo
 
  


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
BASH Shell Scripting -- foreach file in folder??? AC97Conquerer Programming 11 07-06-2011 12:25 AM
shell function inside foreach in Makefile germallon Linux - Software 2 10-14-2009 10:31 AM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 02:07 AM
for, foreach loops mystifying in bash! maryfran Linux - Newbie 4 01-23-2009 10:43 AM
copy directories and files recursively using C shell or bash shell bostonuser Programming 7 06-06-2008 01:24 AM

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

All times are GMT -5. The time now is 03:15 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