Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-22-2005, 07:25 AM   #1
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Rep: Reputation: 30
How do I read from a file in BASH?

Hello all,

I'm trying to do this (which works fine):


LIST="Jupiter Venus Mercury"
for planet in $LIST
echo $planet
echo $n
let "n += 1"


But instead of doing it from the list I provide: LIST="Jupiter Venus Mercury"
I would like it to read from a file that has a complete list.

What do I have to change so it reads from a file in BASH?

Last edited by vous; 03-22-2005 at 07:45 AM.
Old 03-22-2005, 07:48 AM   #2
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Change the first line to LIST=`cat planets.txt`

Where planets.txt contains:

Jupiter Venus Mercury Mars...

This'll also work with each planet on a new line.
Old 03-22-2005, 07:52 AM   #3
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Using command substitution, you can easily loop through the contents of a file. If the file will be more than one line, the only thing different you'll need to do is pipe the output of the file through tr and replace all newlines with spaces.

This will only work, though, if your file does not contain words that have spaces in them.

for planet in `cat "$filename" | tr '\n' ' '`; do
  echo $planet
Old 03-22-2005, 08:38 AM   #4
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,634

Rep: Reputation: 390Reputation: 390Reputation: 390Reputation: 390
Or simpler:
for planet in $(<$filename)
  echo $planet
Old 03-22-2005, 06:51 PM   #5
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Rep: Reputation: 15
files can be easily read using the head command

check the man pages for the command head.


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: read a line from file zokik Linux - General 6 12-10-2008 09:24 AM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM
bash - read from file question sqn Programming 4 04-01-2003 04:30 AM

All times are GMT -5. The time now is 02:42 AM.

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