如何循环字符串并删除字符串数组

How to loop on strings and remove the string array

我在 make 文件中有以下代码,它来自命令字符串数组

apps := $(shell fxt run apps)


go:
    @for v in $(apps) ; do \
        echo inside recipe loop with sh command: $$v ; \
    done

命令returns数组如[app1 app2 appN](里面可能有很多app) 但我需要它是 app1 app2 appN ,知道如何删除数组 [] 吗?

当我 运行 make 我得到以下

inside recipe loop with sh command: [app
inside recipe loop with sh command: app2]

您可以简单地使用带有空替换部分的 subst 命令来删除部分变量内容,如下所示:

apps := $(shell ls)

#add []
apps := [ $(apps) ]
$(info Apps: $(apps))

# replace [ with nothing -> remove it
apps := $(subst [ ,,$(apps))

# replace ] with nothing -> remove it
apps := $(subst ],,$(apps))
$(info Apps: $(apps))

或将 filer-out 用于:

apps :=$(filter-out [ ], $(apps))

取而代之的是两个替换函数。重要提示:在括号之间保留一个 space,因为 filter-out 需要一个单词列表。所以这里命令的模式部分有 2 个词。 输出:

Apps: [ a b c Makefile ]
Apps: a b c Makefile

如果输入的[和第一个单词之间没有space,则必须使用subst命令。

也许您喜欢将两个表达式连接成一个(但可读性较差):

    apps := $(subst ],,$(subst [,,$(apps)))