bash - 如何在 Makefile 目标中使用 Bash 语法?

标签 bash shell makefile

我经常找到Bash语法非常有用,例如过程替换,如 diff <(sort file1) <(sort file2) .

是否可以在 Makefile 中使用这样的 Bash 命令?我在想这样的事情:

file-differences:
    diff <(sort file1) <(sort file2) > $@

在我的 GNU Make 3.80 中,这会报错,因为它使用了 shell而不是 bash执行命令。

最佳答案

来自GNU Make documentation ,

5.3.2 Choosing the Shell
------------------------

The program used as the shell is taken from the variable `SHELL'.  If
this variable is not set in your makefile, the program `/bin/sh' is
used as the shell.

所以将 SHELL :=/bin/bash 放在你的 makefile 的顶部,你应该可以开始了。

顺便说一句:您也可以为一个目标执行此操作,至少对于 GNU Make。每个目标都可以有自己的变量赋值,如下所示:

all: a b

a:
    @echo "a is $$0"

b: SHELL:=/bin/bash   # HERE: this is setting the shell for b only
b:
    @echo "b is $$0"

这将打印:

a is /bin/sh
b is /bin/bash

有关详细信息,请参阅文档中的“特定于目标的变量值”。该行可以位于 Makefile 中的任何位置,不必紧接在目标之前。

关于bash - 如何在 Makefile 目标中使用 Bash 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/589276/

相关文章:

arrays - 用于简单正弦波发生器的Bash脚本阵列算法的问题

python - 使用python执行带变量的linux命令

c - 制作 : Totally ignoring a rule

c++ - 如何合并两个不同的 Makefile?

c++ - C Gnu makefile 我不能用 ar 创建一个静态库

bash - 在后台执行进程,不打印 "Done",并获取 PID

Linux Shell 脚本查找文件并重命名

json - 使用 jq --arg 传递的数字参数与 == 不匹配的数据

shell - 为 IBMi IFS 上的目录中的每个文件递归搜索目录

linux - 遍历 Shell 中 grep 返回的行