1
prebuild-install:
    @rm -rf node_modules/
    @rm -rf package-lock.json
    @export GIT_BRANCH_NAME=$(git branch | sed --quiet 's/* \(.*\)/\1/p')
/bin/sh: 1: Syntax error: Unterminated quoted string
Makefile:34: recipe for target 'prebuild-install' failed
make: *** [prebuild-install] Error 2

I want to set environment variable by using this command

    @export GIT_BRANCH_NAME=$(git branch | sed --quiet 's/* \(.*\)/\1/p')

in Makefile, but get the error shown above. What is wrong in this command? When I take this line and execute it in terminal all works fine.

Jens
  • 69,818
  • 15
  • 125
  • 179
Andrey Radkevich
  • 3,012
  • 5
  • 25
  • 57

1 Answers1

2

Make treats $ as introducing a make variable. You need to use $$(...) instead to pass a literal $ to the shell for command substitution.

Also note that make executes each line in a new shell by default, so exporting variables has no effect on later commands. Usually you would use semicolons or backslash/newline continuations so commands execute with the desired variables in their environment.

 export VARIABLE=value; cmd_1; \
      cmd_2
Jens
  • 69,818
  • 15
  • 125
  • 179