I am trying to install something in tomcat webapp. This is beginning of my install target:

tomcat=`locate --regex "^(/var/lib/tomcat[0-9]{1,2}/webapps/[^/]+/)AppName\.html$" -l 1 | tr -d "\n"`
echo "Tomcat: $tomcat"
# If the string is empty (no path matched) or the path does not exists (that should never really happen)
# terminate
if [ -z "$tomcat" ] || [ ! -f "$tomcat" ]; then 
  echo "Application not found on filesystem."
  exit 404

However this is the output:

tomcat=`locate --regex "^(/var/lib/tomcat[0-9]{1,2}/webapps/[^/]+/)AppName\.html -l 1 | tr -d "\n"`
/bin/sh: 1: Syntax error: Unterminated quoted string
makefile:77: recipe for target 'install' failed
make: *** [install] Error 2

Everyone else claims that you can use ` (backtick) to assign command stdout output into variable. I even used tr -d "\n" to delete all newline characters, may they appear. And the code works flawlessly in shell:

XXXXX@debianvirtualbox:~$ tomcat=`locate --regex "^(/var/lib/tomcat[0-9]{1,2}/webapps/[^/]+/)AppName\.html$" -l 1 | tr -d "\n"`
XXXXX@debianvirtualbox:~$ echo $tomcat

What else is there to fix?


$ signs are notorious in makefiles for causing trouble because they are interpreted as variable markers. Here just doubling the one in the first line should do the trick:


Also have a look at other posts to learn more about makefile escaping issues.

| improve this answer | |
  • There's one other problem probably related to $ sign: echo "Tomcat: $tomcat" acts as echo "Tomcat: omcat" in the output. Doubling the sign produced just Tomcat: $tomcat, but I need to retrieve the variable... – Tomáš Zato - Reinstate Monica Oct 2 '15 at 9:47
  • @TomášZato It's often a good idea to use curly brackets for variables, so as to be clear about where the variable name ends: ${tomcat}. Works in bash as well. – klimpergeist Oct 2 '15 at 10:25
  • You might have to escape this $ as well, because otherwise it will be regarded as a makefile variable instead of a bash variable. – klimpergeist Oct 2 '15 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.