5

I've seen things like echo, mv, if, for, in makefile rules. Some kind of shell commands seem to be recognized. Is this bash? How can I figure out which shell is being used and full list of keywords available to me?

  • I always figured it was the system/user default shell, whatever that is. Haven't seen the docs or anything though. – Zoredache Jul 20 '15 at 19:51
11

It's sh:

The execution line shall then be executed by a shell as if it were passed as the argument to the system() interface

system() uses sh. You can definitely use the keywords of the POSIX Shell Command Language, and any non-keyword commands that you expect to be available on your host platform.

sh on your system may actually be another name for a different shell (like bash), in which case you'd have more options available. That sort of makefile won't be portable, though.

As you ask about GNU make specifically, I'll also note that it permits you to specify a different shell to use in the makefile, but that makefile again won't be portable to other implementations of make. GNU make uses sh by default, as POSIX specifies.

  • 1
    I just tested, and it appears that under GNU make, if the command contains no shell metacharacters, it's executed directly, with no shell at all! – Celada Jul 20 '15 at 19:54
  • 2
    @ it is the words “as if” that are important. The spec is about behaviour (semantics), not about implementation. – ctrl-alt-delor Jul 20 '15 at 20:38
  • A comment only for running GoW make on Windows: - The default SHELL in windows is usually cmd.exe - In Windows SHELL environment variable might be set outside makefile thus altering the makefile behavior - It is recommended to add "SHELL = cmd.exe" or similar in the beginning of makefile to force a known shell syntax for the execution of the makefile – lkanab Apr 30 at 13:24
1

@Michael (+1) said it all (and gave pointers for the details). Nevertheless I will emphasize a pair of details (please correct me):

  1. make actions uses "sh"

  2. if useful in gnumake we can redefine shell

    SHELL = bash

and use bash tricks.

  1. (in a unix based environment) -- mv, echo are commands (we can use them in any shell) if and for are sh/bash depend.

  2. shells have a different behavior in respect to quotations, (and also globs and var expansions...) and make also has some peculiar behavior about them.

  3. in windows, etc, together with the installation of gnumake command, it may be useful to install some sort of a Unix surviving kit (sh / bash, some filters, fileutils, and the commands you normally use in make actions)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.