0

I'm trying to create a makefile var that contains either an env var if it's set or a default value by:

get_tag:
  $(eval TAG=$(shell [[ -z ${DOCKER_TAG} ]] && echo "latest" || echo ${DOCKER_TAG}))
  echo ${TAG}

I think this should make TAG available to other stages and that it should use the DOCKER_TAG env var if it is set and use "latest" if it is not set.

However, when I run the makefile, it gives me this output

$ DOCKER_TAG=test make get_tag
/bin/sh: 1: [[: not found
echo test
test
$ make get_tag
/bin/sh: 1: [[: not found
echo 

Why does it throw an error saying it can't find [[?

Sam Reynolds
  • 89
  • 1
  • 10
  • 2
    `sh` and `bash` are two different shells (or, rather, bash is a specific shell; sh is a specification with minimum features a shell needs to provide to be POSIX-compliant, so any shell started under the "sh" name is not guaranteed to provide more than those minimum baseline features). `[[` is a feature only available in bash. – Charles Duffy Mar 23 '23 at 15:01
  • 1
    Because whatever shell `/bin/sh` is on your machine doesn't have a `[[` command. Use `[ -z "${DOCKER_TAG}" ]` instead. – chepner Mar 23 '23 at 15:01
  • 1
    Set `SHELL:=bash` in your makefile. – Charles Duffy Mar 23 '23 at 15:01
  • 1
    (Whereas if you intend to stick with `sh`, your question should be tagged `sh`, not tagged `bash`). – Charles Duffy Mar 23 '23 at 15:02
  • I suspect this is more complicated than it needs to be, anyway. Are you specifically using GNU `make`, or are you targeting the POSIX `make` specification? – chepner Mar 23 '23 at 15:06
  • 1
    thanks for all the responses, setting `SHELL := bash` fixed my problem – Sam Reynolds Mar 23 '23 at 15:10

0 Answers0