Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-10-2011, 06:38 PM   #1
LQ Newbie
Registered: Dec 2010
Posts: 16

Rep: Reputation: 0
reading a value from a textfile using bash script (beginner problem)

I have a text file that contains a single word and I want to write a bash script that will read the word from the text file... The following is my incorrect attempt, as it assigns the name of the textfile to the variable as opposed to the word stored within the textfile:

(assume I have a text file value.txt that has its contents a single word, say wordone)

for f in value.txt
    echo $f
so the output of the above script is value.txt, however I want it to be wordone.
to summarise: how do I assign the value of the word contained within a textfile to a variable?

Last edited by Tinkster; 06-10-2011 at 08:35 PM.
Old 06-10-2011, 07:16 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
As you've discovered, a for loop takes the values following "in" as literal strings, broken up into words according to your IFS settings (default space/tab/newline). You have to use some kind of substitution to generate a list of values that go there if you want to use it.
for f in $(<value.txt) ; do
  echo "$f"
But when taking the values from a text file, a while+read loop is usually better, because read takes in a line at a time, and word-breaking isn't performed. You also don't need to use command substitution, as simple file redirection will work.
while read f ; do
  echo "$f"
done <value.txt
PS: Please use [code][/code] tags around your code, to preserve formatting and to improve readability.


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
Writing to and reading from a socket from bash script. vouser Linux - Networking 2 03-12-2010 10:07 PM
Beginner question, Bash-script - date kickarzt Programming 15 03-10-2010 12:18 PM
interchanging lines in a textfile in bash script neo2k Linux - Newbie 5 07-28-2009 11:48 AM
reading data from textfile in java? hazeposse Linux - Software 2 07-22-2006 06:00 AM
reading file, bash script marri Programming 3 11-15-2004 09:13 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:21 PM.

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