Handling filenames with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling filenames with spaces
# 1  
Old 09-23-2018
Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1.

My current confusion can be expressed by the following shell script:
Code:
#!/bin/bash

touch "file 1" "file 2"
echo -n "ls: " ; ls

echo ---

for file in "file 1" "file 2" ; do 
  echo $file 
done

echo ---
filesToShow=`ls -Q`
echo filesToShow=$filesToShow     #shows filesToShow:"file 1" "file 2" on the screen
echo ---


for file in $filesToShow ; do
   echo $file
done

I create two files whose names have a space in it. Then I list the directory. Files exist.

Then I echo the filenames to the screen using a for-loop with "file 1" "file 2" as the list. That results as expected in two separate lines with the filenames.

Then I list the directory using ls -Q and save the output in the variable filesToShow and echo the variable to the screen. Result: filesToShow="file 1" "file 2"

Then I use another for-loop providing the list via that variable. The result is that the filenames are split into two parts even though they are enclosed in double quotes. Hence I can't do anything with the files like cp or mv. However, cp * .. works, for example.

(I also tried to use ls -b.)

What am I missing?

Last edited by Ralph; 09-23-2018 at 01:25 PM.. Reason: clarification
# 2  
Old 09-23-2018
The problem is in
Code:
for file in $filesToShow

The unquoted expansion of $filesToShow with the default IFS means the fields will be split by using white space.

What you could do instead is to use an array:
Code:
filesToShow=( file\ * )
for file in "${filesToShow[@]}" 
do
  echo "$file"
done






--
An alternative while using strings:

Code:
filesToShow=`ls -Q`
oldIFS=$IFS
IFS=$'\n'
for file in $filesToShow ; do
   echo "$file"
done
IFS=$oldIFS

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 09-23-2018
Quote:
Originally Posted by Scrutinizer
--
An alternative while using strings:

Code:
filesToShow=`ls -Q`
oldIFS=$IFS
IFS=$'\n'
for file in $filesToShow ; do
   echo "$file"
done
IFS=$oldIFS

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.)

Last edited by Ralph; 09-23-2018 at 03:05 PM..
# 4  
Old 09-23-2018
You seem to be making this harder than it needs to be and are depending on non-portable features (such as ls -Q or shell arrays). The simple way to process all files in the current directory (no matter what characters are included in the filenames [even leading, trailing, and/or embedded <space>s, <tab>s, and <newline>s]) is
Code:
for file in *
do	printf '***%s***\n' "$file"	# or whatever you want to do with each file
done

or, if you just want to process files whose names contain one or more <space> characters:
Code:
for file in *" "*
do	printf '***%s***\n' "$file"	# or whatever you want to do with each file
done

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-23-2018
Thanks. But why are shell arrays a non-portable feature? Do not all shells have them. I'm mainly familiar with Bash.
# 6  
Old 09-23-2018
Shell arrays are not defined by the POSIX standards and do not appear in all shells (even in all shells based on Bourne shell syntax). The Bourne shell (on which most current shells other than the csh based shells) did not include arrays. A future revision of the POSIX standards may include arrays, but no one has proposed adding that feature yet.

Note also that to declare an indexed array in bash, you use:
Code:
declare -a array_name

.
With a 1993 or later Korn shell you declare an indexed array with:
Code:
typeset -a array_name

and you declare an associative array with:
Code:
typeset -A array_name

.
I think some recent versions of bash also have associative arrays, but the version of bash available on macOS High Sierra version 10.13.6 (the OS I use) does not.

I don't know what syntax other shells that support arrays use when declaring them.

Last edited by Don Cragun; 09-23-2018 at 05:15 PM.. Reason: Correct typeset -a for indexed ksh arrays.
# 7  
Old 09-23-2018
I'm using Bash 4.4 on Raspbian and on Kali Linux. Both have associative arrays. They must be declared using
Code:
declare -A array_name

. Indexed arrays can be declared by
Code:
declare -a array_name

but that's optional. Actually, the Bash Reference Manual (4.4) mentions associative arrays so I thought they (at least the indexed arrays) are a standard these days.
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