Review your favorite Linux distribution.
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 02-01-2005, 09:40 AM   #1
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.
for FILE in ./*
  echo $FILE
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.:
[dave@cronus ~]$ echo "  " | grep "  "
[dave@cronus ~]$ echo $?
[dave@cronus ~]$ go="  "
[dave@cronus ~]$ echo $go | grep "  "
[dave@cronus ~]$ echo $?
The first works fine. The second fails, but I can see from the output of 'set' that the double space is held in $go.


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

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

Original Poster
Rep: Reputation: 96

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



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


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 > Forums > Non-*NIX Forums > Programming

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