I'm trying to save the standard exit status of a command in a variable of my makefile, but I don't know how to do it.
I've tried this:
pruebas: @A= $(ls)
...but it doesn't work.
You can execute a command in the shell from the Makefile by using the $(shell command) syntax. If for instance you would want to exit status of the 'ls' command in bash you would type
this might need some explanation: the '> /dev/null 2>&1' part redirects both standard output and standard error to /dev/null so you don't see any output, and then 'echo $?' prints the exit status of the last command. You can try this in the command line to see if it works for you, for instance
should give you
since that is the error 'ls' returns when there is an unknown argument (see the ls man page).
Now how to include this in your makefile? Simply add the line
to your Makefile, note the extra '$' in '$$?'. This is to tell make that $? is a variable in the shell and not a variable in the Makefile.
This should be it! below you can see the Makefile I used to test this behavior.
PS make picks the /bin/sh shell by default, but you can override this by setting
this can be useful for more complicated commands that don't execute in /bin/sh