LinuxQuestions.org
Review your favorite Linux distribution.
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-01-2005, 09:40 AM   #1
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Bash double spaces in filenames


Hi all.

I'm writing a script which deals with a directory of full of files.

If I do e.g.
Code:
for FILE in ./*
  do
  echo $FILE
done
then any files which have two spaces in a row in the filename end up having one of the spaces removed when they are assigned to $FILE. This results in 'file not found' errors later on.

Basically, I need the above loop to work, regardless of how many spaces are in each filename.

I've tried using 'ls -b -Q' (and other ls incantations) instead of ./*, but then $FILE gets assigned each part of each filename, instead of the whole filename, because of the spaces.

Any ideas?

Edit: It appears the problem isn't with the assignment, but with echo. Does anyone know how to stop echo from stripping multiple spaces?

Edit 2: The problem only occurs when echo is used on variables. e.g.:
Code:
[dave@cronus ~]$ echo "  " | grep "  "
  
[dave@cronus ~]$ echo $?
0
[dave@cronus ~]$ go="  "
[dave@cronus ~]$ echo $go | grep "  "
[dave@cronus ~]$ echo $?
1
The first works fine. The second fails, but I can see from the output of 'set' that the double space is held in $go.

Dave

Last edited by ilikejam; 02-01-2005 at 10:14 AM.
 
Old 02-01-2005, 10:18 AM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
put quotes around $file:
Code:
echo "$file"
 
Old 02-01-2005, 10:34 AM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Original Poster
Rep: Reputation: 96
Aaaaaargh.

Yes. That's what's wrong. I just spent the best part of an hour tearing my hair out over this.

Cheers!

Dave

/me has been at his computer far too long and is going to bed now.
 
  


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 scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
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, loops and spaces in filenames shy Programming 5 11-08-2004 07:43 AM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM


All times are GMT -5. The time now is 07:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration