Download your favorite Linux distribution at LQ ISO.
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 11-14-2005, 01:01 PM   #1
Registered: Dec 2003
Location: Milwaukee, WI
Distribution: Gentoo
Posts: 39

Rep: Reputation: 15
bash script with spaces

I am running into one problem with a bash script when the files it's analyzing contain spaces in the name. After some debugging I've narrowed it down to the following. I have a variable that's formed by grep'ing multiple lines out of a file. The contents of the variable are the filenames separated by \n characters (at least that's what it looks like when piping grep into a file. For example:

file name 1.txt
program name

Now I need to perform several operations on each file individually. If I use a for loop this should work nicely.


This code breaks the files up by spaces and \n characters, so in this case I get 6 iterations. If I enclose the $FILENAMES variable in "s, then I get only one string to work with, the entire contents of $FILENAMES in one iteration. What I need to have is $F be 'file name 1.txt' in the first iteration and 'program name' in the second iteration (without the 's of course). What's a good way for me to do this?

Thanks much!
Old 11-14-2005, 01:10 PM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
This is a very common problem.

Here's a link to a reponse I made:

Doing a search in the forums for similar words or phrases ought to give you a ton of other ways.
Old 11-14-2005, 09:26 PM   #3
Registered: Dec 2003
Location: Milwaukee, WI
Distribution: Gentoo
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks, I've been looking for something like this for quite some time but always managed to just find a more convoluted way around in the past. Your hint works great. Only thing I did different is kept the same 'for' loop code (without the quotes) as I need the variable later on and no need to grep the file twice. After using the IFS trick it seems that quoting isn't nearly as touchy in for loops.


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 script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
Spaces in bash parameters rose_bud4201 Programming 5 03-11-2005 06:57 AM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 07:10 AM
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 08:55 PM
bash script ? -- spaces in passed parameters azwr Linux - Newbie 3 06-18-2004 06:57 PM > Forums > Non-*NIX Forums > Programming

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