I want to use the variables from another Makefile under the directory ~/kernelbuild/linux-3.14.37 in my own shell script.

Following is the variables that I need:


The problem is that I am not allowed to change the Makefile, so export variable is not an option here.

Also, I have seen people saying to have a separate file that contains all the common variables need. This is practical but I need to put everything just in my single shell script.

I tried to include . ~/kernelbuild/linux-3.14.37/Makefile in my shell script and I thought I should be able to use those variables, but it gives me following errors:

/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 1: VERSION: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 2: PATCHLEVEL: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 3: SUBLEVEL: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 4: EXTRAVERSION: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 5: NAME: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 17: MAKEFLAGS: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 20: unexport: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 26: unexport: command not found
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 47: syntax error near unexpected token `"$(origin V)",'
/home/jl4347/kernelbuild/linux-3.14.37/Makefile: line 47: `ifeq ("$(origin V)", "command line")'

I think that's because the Makefile is not following the syntax rules for shell script. How can I do that then?

Thanks in advance!

3 Answers 3


If you need just simple things like your example, just grep them out of the Makefile.

For more complicated things, GNU Make has a -p option which prints the database after running Make, which includes all the variable definitions (and a lot more). You can use it together with -n, which causes the actions to not actually run (so nothing gets built). You can grep the variables out of the -p output.


You may try to extract the values with grep and sed. For example:

version=$(grep -m 1 VERSION $filename | sed 's/^.*= //g')

This greps for first occurrence of "VERSION" in Makefile

  • I tried your way, but it gives error like grep: ~/kernelbuild/linux-3.14.37/Makefile: No such file or directory, and I am sure I use the right path. Why is that?
    – Jialun Liu
    Mar 29, 2015 at 19:12
  • Sorry, tilde is not expanded when quoted. You can substitute it with $HOME variable or expand to full path. I corrected my answer.
    – baf
    Mar 29, 2015 at 19:22

Make can read a makefile from stdin, so you can give it a here document that is a makefile. The following is a makefile that includes your kernel makefile and adds a new wildcard target, %.var, whose recipe will output the value of the given make variable. (This assumes you don't have any files or other targets that end in .var, of course).

showvar() {
make -f - ${1}.var <<'EOF'
include $(HOME)/kernelbuild/linux-3.14.37/Makefile

    @echo $($*)

my_version=$(showvar VERSION)
patchlevel=$(showvar PATCHLEVEL)
the_sublevel=$(showvar SUBLEVEL)

Make will replace $* with the stem of the %.var target.

The @echo recipe line begins with an actual TAB character.

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.