Welcome to the most active Linux Forum on the web.
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-24-2013, 07:30 PM   #16
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981

I'm usually a stickler for this kind of thing, but I actually think it doesn't matter too much to worry about filenames with newlines in them, particularly when working directly on the command line or in other quick&dirty situations.

That said, when writing a script that will be used repeatedly, it is generally better to do it correctly the first time. Then you'll never have to worry about correcting it later.

The main issue I have with propofol's script is here:

list=($(ls -1 *.mp3))
This is a completely useless use of ls, even considering that IFS was changed to avoid the parsing ls problem. All ls is doing here is acting as a glorified echo, since the shell is expanding the filenames into an argument list anyway before it even runs.

I will also say that changing IFS globally is generally bad form, and can lead to non-transparent code and hard-to-diagnose errors.

As usual, simple globbing is all you generally need, either to set an array, or processed directly in a for loop.

list=( *.mp3 )
The use of find is a bit more problematic, as it would depend on word splitting to work properly in the above. For that you should avoid command substitution entirely and use a while+read loop, with null separators.

while IFS='' read -r -d '' fname; do
    list+=( "$fname" )
done < <( find ~/mp3folder/ -iname '*.mp3' -print0 )

How can I find and deal with file names containing newlines, spaces or both?

One more point that hasn't been mentioned yet. Never use #!/bin/sh as the shebang unless you specifically need POSIX-compatible portability. If you intend to rely on Bash specific features like arrays, then you need to call it explicitly with #!/bin/bash. Otherwise it won't work properly if the system you run it on doesn't have bash as its system shell.


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
php scripts cant locate filenames with spaces latavish Programming 4 04-13-2011 03:55 PM
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 01:50 PM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 08:10 AM
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 09:55 PM
bash, loops and spaces in filenames shy Programming 5 11-08-2004 08:43 AM > Forums > Non-*NIX Forums > Programming

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