space char shell script problem

Polytropon freebsd at
Sat Aug 23 14:57:54 UTC 2008

On Sat, 23 Aug 2008 10:16:36 -0400, Thomas Dickey <dickey at> wrote:
> spaces won't go away, and since they're legal in filenames, one may as
> well handle them.

Well, it's completely possible to create a file name like:

This is my *favourite* photo from "Cats" \ by Bob & Jane / my wife ~ 2008 `musical'.JPG

What a fun handling this. :-)

Call me old fashioned, but I don't mind making things more
complicated than it should be. The space character is the
command argument separator, as well as / is the root directory
and * is "everything". Applications like xmms can even replace
the _ by a space when showing the filename of an mp3 file (given
that no ID3 tag is provided). So I avoid spaces generally, and
when I get files with spaces, I do convert the names automatically.

> A script like
> 	#!/bin/sh
> 	for x in "$@"
> 	do
> 		echo $x
> 	done
> handles quoting nicely enough (for spaces, anyway).  ls will translate
> some non-printing characters to printable; the 'find' program is a better
> alternative if one must derive the list inside the program.

That's correct; find can provide file names including paths and can
furthermore explude directories from being in the list (-type f).

In your script, $x contains the filename with spaces and should
be passed as one value to the program called.

>From Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list