有没有办法在 makefile SHELL 声明中获取 shell 脚本?

Is there a way to source a shell script in the makefile SHELL declaration?

假设我写:

SHELL:=/usr/bin/env bash

commands:
  source sourceme.sh && alias

但我想将 source sourceme.sh 推回到 SHELL 声明中:

SHELL:=/usr/bin/env bash -c "source sourceme.sh" # or something along these lines

这可以做到吗?如果可以,怎么做?

不,你不能那样做。 Make 获取您的食谱行并将它们有效地发送到 $(SHELL) -c <recipeline> 不幸的是,shell 不接受多个 -c 选项,因此要执行您想要的操作,您需要有一个make 有办法在每个配方行的开头插入该字符串,但没有办法做到这一点。

您可以通过编写自己的包装器从 makefile 外部自己完成:

$ cat wrapper.sh
#!/usr/bin/env bash
shift
source sourceme.sh
eval "$@"

$ cat Makefile
SHELL := wrapper.sh

commands:
        alias