Handling filenames with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling filenames with spaces
# 8  
Old 09-23-2018
Quote:
Originally Posted by Ralph
Both versions worked fine. Why do you write IFS=$'\n' ? It worked for me without the $. But shouldn't now a newline be used to split the fields? (There's no newline in the variable.)
$'\n' is a way to denote a hard newline character. When this variable is set before the unquoted variable expansion of filesToShow, the variable is split into fields depending on the newline character.

After the assignment filesToShow=`ls -Q` there are certainly newlines present in the variable.

If you use IFS='\n' then the content of variable is field split on the n character or the backslash character. So if there happen to be no n-characters or \-characters then the variable will be split into a single field, that gets printed in a a single for loop iteration. What is then printed may look the same, but the process is entirely different..

To illustrate:
Code:
$ IFS='\n'; for file in $filesToShow ; do   echo "new line: $file"; done
new line: file 1
file 2
file 
new line: umber 1
file 
new line: umber 2
file 
new line: umber 3
$ IFS=$'\n'; for file in $filesToShow ; do   echo "new line: $file"; done
new line: file 1
new line: file 2
new line: file number 1
new line: file number 2
new line: file number 3


--
Note1: $'\n' only works in modern shells is also not a standard feature (like is the case with arrays). A portable alternative would be to assign like this:
Code:
IFS="
"

Note2: it is better to use filesToShow=$(ls -Q) instead of the outdated backticks method.

Last edited by Scrutinizer; 09-24-2018 at 01:36 PM..
# 9  
Old 09-24-2018
IFS=$'\n' is a bash invention.
IFS='\n' is plain wrong.?
Portable is
Code:
IFS='
'


Last edited by MadeInGermany; 09-24-2018 at 05:54 AM.. Reason: added icode tags
# 10  
Old 09-24-2018
One thing is special for IFS, if empty it defaults to a newline.
So
Code:
IFS=''

or
Code:
IFS=

is portable, too.

Last edited by MadeInGermany; 09-25-2018 at 10:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

2. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

3. Shell Programming and Scripting

Handling blank spaces

Hi, I am trying to replace a specific column values in a csv file with double quotes when I am find embedded spaces with in the fields. Example: SNO,NAME,ZIPCODE,RANK,SEX,ADDRESS 1,Robert,74538,12,34, M,Robert Street, NY 2,Sam,07564,13,M,12 Main Ave, CA 3,Kim, Ed,12345,14,M,123D ,... (1 Reply)
Discussion started by: techmoris
1 Replies

4. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

5. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

6. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

7. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

8. Shell Programming and Scripting

how to handle spaces in filenames

I'm trying to do something like that: for $filename in `ls -1` do some_command $filename done but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around? (4 Replies)
Discussion started by: rayne
4 Replies

9. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies

10. Shell Programming and Scripting

handling spaces in unix

I am testing a ksh script for email. In the script I receive several parameters. One of them is a subject. The subject may contain spaces. Ex. Test this. When I am running the script on telnet to test, how should the syntax at the command line be written. I have this: ksh ResendE.sh '001111'... (2 Replies)
Discussion started by: supercbw
2 Replies
Login or Register to Ask a Question