Results 1 to 3 of 3
Hello forums!
I've been tinkering with a shell script to partition and restore content to a drive based on a type of file in a given directory. My goal is ...
- 10-13-2008 #1Just Joined!
- Join Date
- May 2007
- Posts
- 15
Whitespace issue
Hello forums!
I've been tinkering with a shell script to partition and restore content to a drive based on a type of file in a given directory. My goal is for my script to assemble several restore images, partition the drive based on the images and to then restore those images to the partitions on the drive. Its going to be a multi-boot drive for troubleshooting different systems. Everything is going fine so long as the path to my "configuration" directory does not contain whitespaces.
Here is a snippet:
The result is unusable:Code:for file in `ls "/test folder"/*.ext`; do echo "$file"; done
I've tried single and double quoting all over the place and I can't find the correct usage. All I'm looking for is this:Code:/test folder/test1.ext /test folder/test2.ext /test folder/test3.ext
The "configuration" folder in my actual script is a variable passed on from another call earlier in the script. I know I could use:Code:/test folder/test1.ext /test folder/test2.ext /test folder/test3.ext
but because the "/test folder" is a variable grepped from the output of a function that only gives human readable output, I'm stuck.Code:for file in `ls /test\ folder/*.ext`; do echo "$file"; done
Thanks for any help - I've been working on this line of my script for 3 days now and I can't figure it out. I'm always in awe of how well some folks have mastered the command line. Thanks again!
- robbie -
- 10-14-2008 #2
So actually, all that your quoting is going to do is mess with the way that find handles the space, and that is actually fine. The real problem here is the way that for loops handle their input. By default, Bash splits on any sort of whitespace, which means newlines, spaces, tabs, etc.
What you want is to only split on newlines. To do this, you set a special variable called IFS. From the Bash man page:
So I suggest changing your code to:Code:IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is ``<space><tab><new- line>''.
This will tell the for loop to only split on newlines.Code:IFS=$'\012'; for file in `ls "/test folder"/*.ext`; do echo "$file"; done
Hope that helps!DISTRO=Arch
Registered Linux User #388732
- 10-14-2008 #3Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
First of all, you don't need "ls". This will also work:
You might also consider using "find". Depending on what you are trying to achieve, you might be able to use a pipeline with "find -print0", "xargs --null" and so on, in order to avoid splitting on whitespace.Code:for file in /usr/local/*; do echo $file; done


Reply With Quote