title: Shell script with CGI works fine with bash but not with sh
body: <p>I have found following working example of CGI with bash. If I change first two lines to </p>
  
  <pre><code>#!/bin/sh
  echo "Content-type: text/html\n\n"
  </code></pre>
  
  <p>following the script stops working and when I browse the script in a browser the 'foo', 'bar' and 'foobar' declared at the bottom of the script disappears.</p>
  
  <p>Any idea how can I make the same example work with sh. Actually I need to run such an example over an embedded device where I don't have bash but sh.</p>
  
  <pre><code>#!/bin/bash
  echo -e "Content-type: text/html\n\n"
  echo "
  &lt;html&gt;
  &lt;body&gt;
  &lt;form action="http://${HTTP_HOST}:${SERVER_PORT}${SCRIPT_NAME}?foo=1234" method="POST"&gt;
  &lt;input type="text" name="bar"&gt;
  &lt;textarea name="foobar"&gt;&lt;/textarea&gt;
  &lt;input type="submit"&gt;
  &lt;/form&gt;"
  
  
  # (internal) routine to store POST data
  cgi_get_POST_vars()
  {
      # check content type
      # FIXME: not sure if we could handle uploads with this..
      [ "${CONTENT_TYPE}" != "application/x-www-form-urlencoded" ] &amp;&amp; \
      echo "Warning: you should probably use MIME type "\
           "application/x-www-form-urlencoded!" 1&gt;&amp;2
      # save POST variables (only first time this is called)
      [ -z "$QUERY_STRING_POST" \
        -a "$REQUEST_METHOD" = "POST" -a ! -z "$CONTENT_LENGTH" ] &amp;&amp; \
      read -n $CONTENT_LENGTH QUERY_STRING_POST
      return
  }
  
  # (internal) routine to decode urlencoded strings
  cgi_decodevar()
  {
      [ $# -ne 1 ] &amp;&amp; return
      local v t h
      # replace all + with whitespace and append %%
      t="${1//+/ }%%"
      while [ ${#t} -gt 0 -a "${t}" != "%" ]; do
      v="${v}${t%%\%*}" # digest up to the first %
      t="${t#*%}"       # remove digested part
      # decode if there is anything to decode and if not at end of string
      if [ ${#t} -gt 0 -a "${t}" != "%" ]; then
          h=${t:0:2} # save first two chars
          t="${t:2}" # remove these
          v="${v}"`echo -e \\\\x${h}` # convert hex to special char
      fi
      done
      # return decoded string
      echo "${v}"
      return
  }
  
  # routine to get variables from http requests
  # usage: cgi_getvars method varname1 [.. varnameN]
  # method is either GET or POST or BOTH
  # the magic varible name ALL gets everything
  cgi_getvars()
  {
      [ $# -lt 2 ] &amp;&amp; return
      local q p k v s
      # get query
      case $1 in
      GET)
          [ ! -z "${QUERY_STRING}" ] &amp;&amp; q="${QUERY_STRING}&amp;"
          ;;
      POST)
          cgi_get_POST_vars
          [ ! -z "${QUERY_STRING_POST}" ] &amp;&amp; q="${QUERY_STRING_POST}&amp;"
          ;;
      BOTH)
          [ ! -z "${QUERY_STRING}" ] &amp;&amp; q="${QUERY_STRING}&amp;"
          cgi_get_POST_vars
          [ ! -z "${QUERY_STRING_POST}" ] &amp;&amp; q="${q}${QUERY_STRING_POST}&amp;"
          ;;
      esac
      shift
      s=" $* "
      # parse the query data
      while [ ! -z "$q" ]; do
      p="${q%%&amp;*}"  # get first part of query string
      k="${p%%=*}"  # get the key (variable name) from it
      v="${p#*=}"   # get the value from it
      q="${q#$p&amp;*}" # strip first part from query string
      # decode and evaluate var if requested
      [ "$1" = "ALL" -o "${s/ $k /}" != "$s" ] &amp;&amp; \
          eval "$k=\"`cgi_decodevar \"$v\"`\""
      done
      return
  }
  
  
  
  # register all GET and POST variables
  cgi_getvars BOTH ALL
  
  echo "&lt;pre&gt;foo=$foo&lt;/pre&gt;"
  echo "&lt;pre&gt;bar=$bar&lt;/pre&gt;"
  echo "&lt;pre&gt;foobar=$foobar&lt;/pre&gt;"
  
  echo "&lt;/body&gt;
  &lt;/html&gt;"
  </code></pre>
  
  <p><strong>Update 1:</strong>
  <code>sh -x script</code> returned following:</p>
  
  <pre><code>+ echo Content-type: text/html\n\n
  Content-type: text/html
  
  
  + echo 
  &lt;html&gt;
  &lt;body&gt;
  &lt;form action=http://:?foo=1234 method=POST&gt;
  &lt;input type=text name=bar&gt;
  &lt;textarea name=foobar&gt;&lt;/textarea&gt;
  &lt;input type=submit&gt;
  &lt;/form&gt;
  
  &lt;html&gt;
  &lt;body&gt;
  &lt;form action=http://:?foo=1234 method=POST&gt;
  &lt;input type=text name=bar&gt;
  &lt;textarea name=foobar&gt;&lt;/textarea&gt;
  &lt;input type=submit&gt;
  &lt;/form&gt;
  + cgi_getvars BOTH ALL
  + [ 2 -lt 2 ]
  + local q p k v s
  + [ ! -z  ]
  + cgi_get_POST_vars
  + [  != application/x-www-form-urlencoded ]
  + echo Warning: you should probably use MIME type  application/x-www-form-urlencoded!
  Warning: you should probably use MIME type  application/x-www-form-urlencoded!
  + [ -z  -a  = POST -a ! -z  ]
  + return
  + [ ! -z  ]
  + shift
  + s= ALL 
  + [ ! -z  ]
  + return
  + echo &lt;pre&gt;foo=&lt;/pre&gt;
  &lt;pre&gt;foo=&lt;/pre&gt;
  + echo &lt;pre&gt;bar=&lt;/pre&gt;
  &lt;pre&gt;bar=&lt;/pre&gt;
  + echo &lt;pre&gt;foobar=&lt;/pre&gt;
  &lt;pre&gt;foobar=&lt;/pre&gt;
  + echo &lt;/body&gt;
  &lt;/html&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>