title: Strip all spaces from a variable and append to another variable if not blank
body: <p>I'm using make in Windows and I need to manipulate some variables to create a filename. I have a base filename and an optional part to append. If the optional part is appended an underscore should be inserted between two parts. The optional part is being read in from a file and contains 5 characters (spaces or alphanumeric).</p>
  
  <p>This is what I expected to work:</p>
  
  <pre><code>OPTIONAL_NAME="     "
  BASE_NAME=myfile
  STRIPPED_OPTIONAL_NAME=$(strip $(OPTIONAL_NAME))
  ifeq ("$(STRIPPED_OPTIONAL_NAME)","")
  FULL_NAME=$(BASE_NAME)
  else
  FULL_NAME=$(BASE_NAME)_$(STRIPPED_OPTIONAL_NAME)
  endif
  </code></pre>
  
  <p>OPTIONAL_NAME could have the following values:</p>
  
  <pre><code>OPTIONAL_NAME="     "
  OPTIONAL_NAME="ABCDE"
  OPTIONAL_NAME="A    "
  OPTIONAL_NAME="  A  "
  OPTIONAL_NAME="    A"
  </code></pre>
  
  <p>The value of OPTIONAL_NAME has quotes around it due to the way it is being imported (see my previous question - <a href="https://stackoverflow.com/questions/12180162/read-a-value-from-a-file-into-a-variable">Read a value from a file into a variable</a>).</p>
  
  <p>The problem with the result is that <code>strip</code> seems to reduce multiple spaces to single spaces, but not actually remove leading or trailing spaces.</p>
  
  <p>I've tried using this to strip the spaces:</p>
  
  <pre><code>space:=
  space+=
  STRIPPED_OPTIONAL_NAME=$(subst $(space),,$(OPTIONAL_NAME))
  </code></pre>
  
  <p>This does remove the spaces, but I struggle to check for an empty string.</p>
  
  <p>Thanks</p>
  
  <p>Stephen</p>