A potential way to have spaces in filenames and not break the Unix command line

A thread at Hacker News about using makefiles for JavasScript [1] was about the difficulty in using filenames with spaces [2]. And yes, it is difficult. A Makefile such as:

hello world:

Results in:

[spc]lucy:/tmp/foo>make
make: Nothing to be done for `hello'.
[spc]lucy:/tmp/foo>

Enclosing the filename with quotes (single or double, it doesn't matter) gives the same results. If I escape the space like:

hello\ world:

make gives:

[spc]lucy:/tmp/foo>make
cc     hello world.c   -o hello world
cc: hello: No such file or directory
cc: world.c: No such file or directory
cc: world: No such file or directory
cc: no input files
make: *** [hello world] Error 1
[spc]lucy:/tmp/foo>

So yes, using a filename with spaces is problematic with make. Part of that is the unique nature of the space character [3]. In ASCII (American Standard Code for Information Interchange), it's grouped next to the information separator characters and thus, could be treated as another type of separator character (and on input, it usually is considered such). It could also be considered a control character as a format effector such that it causes the character position to advance one place to the right (and is thus used as such with output).

It's the prevasive use of space as a separator in Unix that causes the most issues, such with make, and the command line in general.

But there is a solution …

[spc]lucy:/tmp/foo>ll
total 12
-rw-r–r–  1 spc spc  14 Feb 28 18:13 Makefile
-rw-r–r–  1 spc spc  76 Feb 28 18:13 hello world.c
-rw-r–r–  1 spc spc 227 Feb 28 18:13 x.lua
[spc]lucy:/tmp/foo>cat Makefile
hello world:
[spc]lucy:/tmp/foo>make
cc     hello world.c   -o hello world
[spc]lucy:/tmp/foo>./hello world
Hello, world!
[spc]lucy:/tmp/foo>

No, the output is not faked. Yes, the filename is hello world.c. The name, however, is not pure ASCII—it contains the Unicode [4] character for a “non-breaking space [5]”. Cheating? Perhaps. But it is defined as a space (graphically), and more importantly, it's not considered an information separator by Unix utilties. It also requires a filesystem that can support Unicode (or in my case, UTF-8 [6]) and a command line that also supports Unicode (or again in my case, UTF-8). And it was also not that easy to create the filename and Makefile with the non-breaking space.

But other than those minor issues [Ha! —Editor], hey—spaces! In filenames!

[1] https://news.ycombinator.com/item?id=16483889

[2] https://news.ycombinator.com/item?id=16485411

[3] http://www.aivosto.com/vbtips/control-characters.html#SP

[4] https://en.wikipedia.org/wiki/Unicode

[5] https://en.wikipedia.org/wiki/Non-breaking_space

[6] https://en.wikipedia.org/wiki/UTF-8

Gemini Mention this post

Contact the author