title: How can I escape an arbitrary string for use as a command line argument in Bash?
body: <p>I have a list of strings and I want to pass those strings as arguments in a single Bash command line call. For simple alphanumeric strings it suffices to just pass them verbatim:</p>
  
  <pre><code>&gt; script.pl foo bar baz yes no
  foo
  bar
  baz
  yes
  no
  </code></pre>
  
  <p>I understand that if an argument contains spaces or backslashes or double-quotes, I need to backslash-escape the double-quotes and backslashes, and then double-quote the argument.</p>
  
  <pre><code>&gt; script.pl foo bar baz "\"yes\"\\\"no\""
  foo
  bar
  baz
  "yes"\"no"
  </code></pre>
  
  <p>But when an argument contains an exclamation mark, this happens:</p>
  
  <pre><code>&gt; script.pl !foo
  -bash: !foo: event not found
  </code></pre>
  
  <p>Double quoting doesn't work:</p>
  
  <pre><code>&gt; script.pl "!foo"
  -bash: !foo: event not found
  </code></pre>
  
  <p>Nor does backslash-escaping (notice how the literal backslash is present in the output):</p>
  
  <pre><code>&gt; script.pl "\!foo"
  \!foo
  </code></pre>
  
  <p>I don't know much about Bash yet but I know that there are other special characters which do similar things. <strong>What is the general procedure for safely escaping an arbitrary string for use as a command line argument in Bash?</strong> Let's assume the string can be of arbitrary length and contain arbitrary combinations of special characters. I would like an <code>escape()</code> subroutine that I can use as below (Perl example):</p>
  
  <pre><code>$cmd = join " ", map { escape($_); } @args;
  </code></pre>
  
  <p>Here are some more example strings which should be safely escaped by this function (I know some of these look Windows-like, that's deliberate):</p>
  
  <pre><code>yes
  no
  Hello, world      [string with a comma and space in it]
  C:\Program Files\ [path with backslashes and a space in it]
  "                 [i.e. a double-quote]
  \                 [backslash]
  \\                [two backslashes]
  \\\               [three backslashes]
  \\\\              [four backslashes]
  \\\\\             [five backslashes]
  "\                [double-quote, backslash]
  "\T               [double-quote, backslash, T]
  "\\T              [double-quote, backslash, backslash, T]
  !1                
  !A                
  "!\/'"            [double-quote, exclamation, backslash, forward slash, apostrophe, double quote]
  "Jeff's!"         [double-quote, J, e, f, f, apostrophe, s, exclamation, double quote]
  $PATH             
  %PATH%            
  &amp;                 
  &lt;&gt;|&amp;^             
  *@$$A$@#?-_       
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Would this do the trick? Escape every unusual character with a backslash, and omit single or double quotes. (Example is in Perl but any language can do this)</p>
  
  <pre><code>sub escape {
      $_[0] =~ s/([^a-zA-Z0-9_])/\\$1/g;
      return $_[0];
  }
  </code></pre>