关于bash:makefile中的for循环不起作用

For loop in makefile not working

在我的makefile中,我有:

1
2
3
4
all:
  for i in {20..50000..10} ; do \\
    echo"Computing $$i" ;\\
  done

应该在单独的行上分别打印数字20、30、40,...,50000。

这在Debian oldstable(GNU Make 4.0,GNU Bash 4.3)下有效,但在Debian稳定版(GNU Make 4.1和GNU Bash 4.4.12)下无效。

Debian稳定版仅打印字符串" {20..50000..10} "。为什么是这样?在makefile文件中将此for循环编写的可移植方式是什么?


如果在shell提示符下运行此命令:

1
/bin/sh -c 'for i in {20..5000..10}; do echo $i; done'

您会发现它不符合您的期望。 Make总是调用/bin/sh(应该是POSIX shell)来运行配方:如果它使用调用makefile的人碰巧正在使用的任何shell,那么对于可移植性将是一场灾难。

如果您真的想用bash语法编写makefile配方,则必须通过添加以下内容来明确要求:

1
SHELL := /bin/bash

到您的makefile。


与POSIX兼容的循环粘连:

1
2
3
4
all:
  i=20; while ["$$i" -le 50000 ]; do \\
    echo"Computing $$i"; i=$$((i + 10));\\
  done