n1te
n1te

Reputation: 945

How to assign the output of a command to a Makefile variable while preserving the new line characters

I'd like to assign the output of a command to a Makefile variable, but the constraint is that I need to preserve the new line characters.

The value is a private key, hence the following code will result in a malformed key:

SHELL=/bin/bash

APP_NAME?=foo

ifdef CI
    export DEPS_PRIVATE_KEY=$(shell echo $(CI_BASE64_PRIVATE_KEY) | base64 -d)
endif

build:
    @docker build --no-cache --build-arg DEPS_PRIVATE_KEY -t $(APP_NAME) .
.PHONY: build

I realize that I can set the value in bash and then call make build like so:

DEPS_PRIVATE_KEY="$(echo $CI_BASE64_PRIVATE_KEY | base64 -d)" make build

but I was wondering if it's possible to encapsulate this logic in the Makefile.

Any help much appreciated.

Upvotes: 3

Views: 703

Answers (1)

Renaud Pacalet
Renaud Pacalet

Reputation: 29050

Using a make variable as an intermediate for a shell environment variable -- especially when the values are multi-line strings -- is probably uselessly complex. The simplest is thus probably to add the environment variable definition to your recipe:

$ cat Makefile
build:
    @export TXT="$$(echo "$$(B64)" | base64 -d)"; \
    printenv TXT
$ make B64="$(printf 'a\nb\n' | base64)"
export TXT="$(echo "YQpiCg==" | base64 -d)"; \
printenv TXT
a
b

Upvotes: 2

Related Questions