title: BASH: Unavoidable wordsplitting in subcommand expansion?
body: <p>So I'm writing a BASH shell script to perform some CLI testing for a Node project I'm working on (I didn't tag Node in this question because really this solely pertains to BASH); my the CLI testing looks like this:</p>
  
  <pre class="lang-sh prettyprint-override"><code>test_command=$'node source/main.js --input-regex-string \'pcre/(simple)? regex/replace/vim\' -o';
  echo $test_command;
  $test_command 1&gt;temp_stdout.txt 2&gt;temp_stderr.txt;
  test_code=$?;
  echo "test_code $test_code"
  test_stdout=`cat temp_stdout.txt`;
  test_stderr=`cat temp_stderr.txt`;
  </code></pre>
  
  <p>As you can see, I'm using the C-style quotes <code>$'...'</code>, as <a href="https://wiki.bash-hackers.org/syntax/quoting" rel="nofollow noreferrer">described here</a>, which should make it so that <code>$test_command</code> expands literally to <code>node source/main.js --input-regex-string 'pcre/(simple)? regex/replace/vim' -o</code> which <em>is</em> what the <code>echo</code> on line 2 shows, however when I attempt to run the command on line 3, I'll get an error saying that <code>regex/replace/vim'</code> isn't a recognised command-line parametre in my script. Obviously, what's happening here is despite me seemingly quoting and escaping everything correctly, BASH is still splitting the <code>regex/replace/vim'</code> part into its own <a href="https://wiki.bash-hackers.org/syntax/words" rel="nofollow noreferrer">word</a>. Based on everything I've read on the topic of BASH's quoting and word splitting rules, this shouldn't be happening but yet it is. I've tried changing the quoting on the first line to use strong/literal <code>'</code> quotes (<code>'node source/main.js --input-regex-string "pcre/(simple)? regex/replace/vim" -o'</code> which just causes line 3 to treat the entire thing as one word and thus not work) and the weak/dynamic <code>"</code> quotes (<code>"node source/main.js --input-regex-string 'pcre/(simple)? regex/replace/vim' -o"</code> exact same as strong-quote example, not to mention that since the quoted string in this case is a regular expression literal, it's not a good fit for the magic expansion behaviour of <code>"</code> anyway) in place of the C-style quotes, changing the escaping of the command string itself to fit with whichever quote style is being used; I've tried adding additionally escaping to the string such as <code>test_command=$'node source/main.js --input-regex-string \\\'pcre/(simple)?\ regex/replace/vim\\\' -o</code> only to witness the exact same behaviour; and I've tried changing the way I invoke the command on line 3: quoting the expansion, encasing it in <code>{ ... }</code> or <code>${ ... }</code> with combinations of the previously mentioned variations, all of which still resulted in either original word-splitting problem or me just being given a generic "bad substitution" syntax error.</p>
  
  <p><strong>So, in short,</strong> my question is what is the correct way to invoke/format a command, stored as a string in a BASH variable, containing a quoted literal string, that BASH won't inexplicably word split the contained quoted string and break the whole command?</p>