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– Zoredache2015-07-20 19:51:23 +00:00Commented Jul 20, 2015 at 19:51
3 Answers
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.
-
2I 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– Celada2015-07-20 19:54:29 +00:00Commented Jul 20, 2015 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– ctrl-alt-delor2015-07-20 20:38:22 +00:00Commented Jul 20, 2015 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 makefilelkanab– lkanab2019-04-30 13:24:03 +00:00Commented Apr 30, 2019 at 13:24
-
Hey Michael, what exactly is that documentation that you've linked for
sh? When talking about Linux stuff, it is very common for me to see somebody linking a doc and I do not understand what exactly is that documentation. Is it official documentation for make, for sh, or else? I'm trying to understand what exactly I'm reading there.Rafael Eyng– Rafael Eyng2020-06-12 16:36:22 +00:00Commented Jun 12, 2020 at 16:36 -
1@RafaelEyng: Four years late, but any URL beginning with
pubs.opengroup.orgis (most of the time) a link to the POSIX standard. Technically, it's not documentation at all, but the vast majority of Unix-like systems are at least mostly conformant to POSIX, so it is helpful as a baseline for what you can expect on a wide variety of different operating systems.Kevin– Kevin2024-08-01 03:26:00 +00:00Commented Aug 1, 2024 at 3:26
You should also look at PolyGlot Makefiles, a short note on selecting the shell for particular makefile targets.
Here's selecting bash for a target named bash:
.ONESHELL:
bash: SHELL := bash
bash:
# note: the double dollar-sign is required because Make substitues $variables
export greeting="¡hola"
echo "$${greeting}, bash!"
If the
.ONESHELLspecial target appears anywhere in the makefile then all recipe lines for each target will be provided to a single invocation of the shell. GNU make - 5.3.1 Using One Shell
And this is just showing off:
.ONESHELL:
python: SHELL := python3
python:
greeting = "hello"
print(f"{greeting}, python!")
So, make python then prints 'hello, python!'.
@Michael (+1) said it all (and gave pointers for the details). Nevertheless I will emphasize a pair of details (please correct me):
make actions uses "sh"
if useful in gnumake we can redefine shell
SHELL = bash
and use bash tricks.
(in a unix based environment) --
mv,echoare commands (we can use them in any shell)ifandforare sh/bash depend.shells have a different behavior in respect to quotations, (and also globs and var expansions...) and
makealso has some peculiar behavior about them.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)
-
Could you elaborate on
and use bash tricks? What exactly are bash tricks?Rafael Eyng– Rafael Eyng2020-06-12 16:38:15 +00:00Commented Jun 12, 2020 at 16:38