note
Anonymous Monk
<blockquote>
<em>In trivial cases like this one, where either type of quotes will work around the Perl string, simply swapping the internal double quotes with the external single quotes can fix the problem. That's because double quotes, unlike single quotes, are recognized as quoting characters by Windows, permitting this reworked command to work as intended:</em>
<code>
perl -wl -e "print 'Crikey, what a little beauty!';"
</code>
</blockquote>
First of all, which kind of quotes you should use isn't a matter of the OS. It's a matter of the shell you are using.
And there are good reasons to use single quotes in Unix shells. Because, even in trivial cases as your example, <em>using double quotes won't work as you may expect</em>. See, Perl borrowed a lot from Unix and the shell. A whole lot. Including the difference between single and double quotes. Running your example in bash, a not uncommon Unix shell, gives:
<code>
$ perl -wl -e "print 'Crikey, what a little beauty!';"
-bash: !': event not found
</code>
And in csh:
<code>
$ perl -wl -e "print 'Crikey, what a little beauty!';"
';": Event not found.
</code>
And in zsh:
<code>
$ perl -wl -e "print 'Crikey, what a little beauty!';"
zsh: no such event: 0
</code>
For this example, using double quotes works fine in the Bourne shell, ash, ksh, and tcsh.
Hence, the use of double quotes instead of single quotes isn't portable between shells on a Unix system - good enought reason in itself to use double quotes. Had the example contained an actual scalar variable, the use of double quotes in any Unix shell would have caused a problem: just like in Perl, Unix shells interpolate inside double quotes, and don't interpolate inside single quotes.
<p>
So, given 8 shells, sh, ash, bash, ksh, zsh, csh, tcsh and the standard Windows shell, use of single quotes make my one-liner work on 7 of them. Use of double quotes is going to fail on several of them if the command line contains an exclaimation mark, and is going to fail on 7 of them if it contains a scalar variable. Now, if my goal was to maximize portability, I'd pick the solution that works 7 out of 8 times instead of 1 out of 8 times.
<p>
And guess what? Various shells, including bash, have been ported to different operating systems, including Windows. And having a Bourne like shell is a requirement for POSIX compliance anyway.
581633
581633
39