Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call bash function inside makefile

I have an impression that I can call bash function inside GNU makefile, but seems wrong. Here is a simple test, I have this function defined:

>type lsc
lsc is a function
lsc () 
{ 
    ls --color=auto --color=tty
}

Here is my Makefile:

>cat Makefile
all:
    lsc

Here is what I get in running make:

>make
lsc
make: lsc: Command not found
make: *** [all] Error 127

Is my impression wrong? Or is there any env setup issue? I can run "lsc" at the command line.

like image 743
my_question Avatar asked Oct 28 '12 13:10

my_question


3 Answers

Use $* in your BASH script:

functions.sh

_my_function() {
  echo $1
}

# Allows to call a function based on arguments passed to the script
$*

Makefile

test:
    ./functions.sh _my_function "hello!"

Running example:

$ make test
./functions.sh _my_function "hello!"
hello!
like image 146
luismartingil Avatar answered Nov 06 '22 01:11

luismartingil


You cannot call bash functions or aliases in a Makefile, only binaries and scripts. What you can do however, is calling an interactive bash and instruct it to call your function or alias:

all:
    bash -i -c lsc

if lsc is defined in your .bashrc, for example.

like image 37
Olaf Dietsche Avatar answered Nov 05 '22 23:11

Olaf Dietsche


Did you export your function with "export -f"?

Is bash the shell of your Makefile, or is is sh?

like image 2
Sebastian Avatar answered Nov 06 '22 00:11

Sebastian