Contents|Index|Previous|Next

The shell Function

The shell function is unlike any other function except the wildcard function (see The Function, wildcard) in that it communicates with the world outside of make.

The shell function performs the same function that backquotes (‘‘’) perform in most shells: it does command expansion. This means that it takes an argument that is a shell command and returns the output of the command. The only processing make does on the result, before substituting it into the surrounding text, is to convert newlines to spaces.

The commands run by calls to the shell function are run when the function calls are expanded. In most cases, this is when the makefile is read in. The exception is that function calls in the commands of the rules are expanded when the commands are run, and this applies to shell function calls like all others. The following is an example of the use of the shell function which sets contents to the contents of the file, ‘foo’, with a space (rather than a newline) separating each line.

contents := $(shell cat foo)

The following is an example of the use of the shell function which sets files to the expansion of ‘*.c’. Unless make is using a very strange shell, this has the same result as ‘$(wildcard *.c)’.

files := $(shell echo *.c)