Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 18 Oct 2013 07:09:03 +0200
From:      Polytropon <freebsd@edvax.de>
To:        Johan Kuuse <kuuse@redantigua.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: FreeBSD Make question
Message-ID:  <20131018070903.7ef5f8a8.freebsd@edvax.de>
In-Reply-To: <CAGUU1d2T5QMWF619KXgoMwtAG1iA4zcL3KFuCcg-tdOZBrXSGg@mail.gmail.com>
References:  <CAGUU1d2T5QMWF619KXgoMwtAG1iA4zcL3KFuCcg-tdOZBrXSGg@mail.gmail.com>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
On Thu, 17 Oct 2013 18:43:33 +0200, Johan Kuuse wrote:
> Hi,
> 
> I'm trying to write a Makefile for FreeBSD Make (not GNU Make), with target
> names containg spaces.
> Example:
> 
> MY_TARGET=/home/joe/directory name with spaces/hello.c
> ${MY_TARGET}:
>     @echo ${.TARGET}
> 
> The output is truncated to '/home/joe/directory'

That is to be expected. :-)

The space character is a _special_ character. It serves as
a statement separator. (There are other special characters
depending for example on the shell in use; other systems
have different special characters that _could_ be valid in
directory names or file names, but _should_ not be used
because they could cause trouble when _improperly_ dealt
with.)



> Is there any possible way to escape this properly?

There are, in fact, many possibilities.

In an "O(n) manner" you can use the backslash \ to escape
each of the spaces. They hereby lose their special meaning
of being a statement separator:

MY_TARGET=/home/joe/directory\ name\ with\ spaces/hello.c

In an "O(1) manner" you can enclose the whole string in
double quotes "...":

MY_TARGET="/home/joe/directory name with spaces/hello.c"

Single quotes '...' work similarly, with the exception that _if_
your string contains variables, they would not be expanded,
but in your example, this does not apply.

MY_TARGET='/home/joe/directory name with spaces/hello.c'

The so-called backticks `...` have a totally different meaning
(subshell result) and will not be considered here. :-)



> I have read all the documentation I could find, and tried several ways
> solving this problem, using quotes, escapes, substitutions.

Note that even if you get the above statement working, there
could be further annoying trouble ahead! If you intend to use
special characters in file names (and directory names), there
are a lot things you have to pay attention to.

I suggest having a read of the following articles:

David A. Wheeler:
Filenames and Pathnames in Shell:
How to do it correctly

http://www.dwheeler.com/essays/filenames-in-shell.html

as well as

David A. Wheeler:
Fixing Unix/Linux/POSIX Filenames:
Control Characters (such as Newline), Leading Dashes,
and Other Problems

http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html



> The output is the same if as use sh, bash, or tcsh, so it isn't shell
> related.

The Makefile executes a shell (usually sh) for each command
to be executed. It handles its own statements "internally"
(declaring dependencies and such).



> Are spaces simply not possible to use in target names?

They are possible, but you should not use them. It's also
possible to use ~, *, newline, ; or - in file names, but
you really _really_ should not do this. :-)





-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?20131018070903.7ef5f8a8.freebsd>