Variables 未从grep输出设置Makefile变量

Variables 未从grep输出设置Makefile变量,variables,makefile,grep,pipe,Variables,Makefile,Grep,Pipe,我试图将变量COGLINE设置为我的grep行的输出(它正在我的config.json文件中搜索regExthe“cogs”)。当我执行grep行时,它会正确地输出正确的行号,但当我回显变量时,它会显示为空 COGLINE = $(grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:) all: grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json

我试图将变量COGLINE设置为我的grep行的输出(它正在我的config.json文件中搜索regExthe“cogs”)。当我执行grep行时,它会正确地输出正确的行号,但当我回显变量时,它会显示为空

COGLINE = $(grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:)

all:
    grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:
    echo $(COGLINE)
以下是输出:

GlennMBP:test glenn$ make all
grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:
2
echo 

您可以看到,行号正确地显示为“2”,但变量显示为空,就好像没有设置一样。我做错了什么?

grep
不是make函数。该
COGLINE=
line是一个make赋值

你要么需要使用

COGLINE := $(shell grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:)
如果您希望在make解析时运行它,并希望它位于make变量中

all
recipe执行时运行它,并将其保存在shell变量中

也有中间立场,但这是两个基本观点

all:
        COGLINE=$$(grep -n \"cogs\" ~/Desktop/Repos/pronghorn/config.json | cut -f1 -d:); \
        echo "$${COGLINE}"