LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 02-15-2014, 11:46 AM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
bash: variable is a filename not a list


I have a list of three filenames:

Code:
this_is_a_filename
this
this is a filename
If i do:

Code:
for i in `cat /tmp/l`; do echo $i; done
I get:

Code:
this_is_a_filename
this
this
is
a
filename
Which is 6 elements and I want 3. How can I preserve the name of the file "this is a filename" ? It seems bash is interpreting this filename as a list.
 
Old 02-15-2014, 12:42 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
There's probably several approaches that can be used to resolve the "space in file name" issue. Here's one simple solution (but perhaps not the best):
Code:
IFS=$'\n'
for i in `ls`; do echo $i; done
unset IFS
Edit:

I just realized I slapped an 'ls' in the example above. I should have copied/pasted the OP's code. Doh!

Last edited by dwhitney67; 02-15-2014 at 03:32 PM.
 
Old 02-15-2014, 12:58 PM   #3
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
Thanks! I think this will do it!
 
Old 02-15-2014, 12:59 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
for i in `cat /tmp/l`; do echo $i; done
This method is over-used by beginners, but it has serious flaws. Try this:

Code:
while read i; do echo "$i"; done </tmp/l

Last edited by NevemTeve; 02-15-2014 at 01:00 PM.
 
Old 02-15-2014, 01:07 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
while read fn;do
    echo "${fn}"
done </tmp/1
don't use `ls` in a for loop

Code:
for fn in *;do
    echo "${fn}"
done
http://mywiki.wooledge.org/BashGuide/Patterns
 
  


Reply



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
How can I extract the .rpm filename from long list of entries using bash shell script Glenn D. Linux - Software 2 07-14-2012 05:42 AM
replace filename with variable Siljrath Programming 2 11-12-2009 12:40 AM
Read filename as a variable kicap Programming 3 08-12-2009 04:27 PM
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:09 PM.

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
Open Source Consulting | Domain Registration