title: Bash parameter quotes and eval
body: <p>I've written a bash logging library to be implemented with some complex scripts that my company is currently using. I've been deadset on providing the script filename (${BASH_SOURCE}) and the line number (${LINENO}) of the calling script when making the log calls. However, I didn't want to have to rely on the user or implementing script to pass in these two variables as parameters. If this were C/C++, I would just create a macro that prepends "__FILE__" and "__LINE__" to the parameter list.</p>
  
  <p>I was finally able to get this part working. Here are some much-simplified abstracts as a proof of concept:</p>
  
  <p>Here's the logging library:</p>
  
  <pre><code># log.sh
  
  LOG="eval _log \${BASH_SOURCE} \${LINENO}"
  
  _log () {
      _BASH_SOURCE=`basename "${1}"` &amp;&amp; shift
      _LINENO=${1} &amp;&amp; shift
  
      echo "(${_BASH_SOURCE}:${_LINENO}) $@"
  }
  </code></pre>
  
  <p>And an implementing test script:</p>
  
  <pre><code># MyTest.sh
  
  . ./log.sh
  
  ${LOG} "This is a log message"
  # (test.sh:5) This is a log message
  </code></pre>
  
  <p>This works pretty well (and, I was thrilled to get it working at first). However, this has one glaring problem: the interaction between quotes and eval. If I make the call:</p>
  
  <pre><code>${LOG} "I'm thrilled that I got this working"
  # ./test.sh: eval: line 5: unexected EOF while looking for matching `''
  # ./test.sh: eval: line 6: syntax error: unexpected end of file
  </code></pre>
  
  <p>Now, I believe that I understand why this is happening. The quoted parameters are kept intact as they're passed to eval, but at that point, the content is placed as-is into the resulting command string. I know that I can fix this by doing some escaping; however, I <strong><em>REALLY</em></strong> do not want to force the implementing scripts to have to do this. Before I implemented this "eval macro" capability, I had users make calls directly to "_log" and allowed them to optionally pass in "${LINENO}." With this implementation, The failing call above (with only a quoted sentence) worked just fine.</p>
  
  <p>At the most basic level, all I really want is for a script to be able to call <em>[log function/macro] "String to log with special characers"</em> and have the resulting log message contain the filename and line number of the calling script, followed by the log message. If it's possible, I would assume that I'm very close, but if there's something I'm overlooking that would require a different approach, I'm open to that as well. I can't force the users to escape all of their messages, as that will likely cause them to not use this library. This is such a problem that if I can't find a solution to this, I'll likely revert to the old capability (which required ${LINENO} to be passed as a function parameter - this was much less intrusive).</p>
  
  <p><strong>TLDR</strong>: Is there any way to get eval to respect special characters within a quoted parameter, without having to escape them?</p>