question: text substitution using Perl

Kevin D. Clark kclark at elbrysnetworks.com
Mon Oct 23 15:25:57 EDT 2006


Zhao Peng writes:

> Just one more question:
>
> "To the shell, anything inside of single quotes is a single argument,
> so all of the spaces, newlines, etc. in there are passed without shell
> interpretation" (quoted from the Mike's answer to my 2nd question).
>
> Can this claim also be extended for double quotes?

Yes, but you need to understand that single quotes have different
semantics than double quotes.

These are all different:

      $FOO
     "$FOO"
     '$FOO'

In double quotes variable interpolation is performed.  You would do
well to understand all of these scenerios:


(look at how FOO is quoted at the end of each line)

  $ FOO="zero one two three four" 


#######################################################
  
  $ perl -le '$i=0; print "\n\nNumber of command line arguments: ", scalar(@ARGV); map { print "argv[$i] = :", $ARGV[$i++], ":"; } @ARGV' FOO
    
  Number of command line arguments: 1
  argv[0] = :FOO:
  
########################################################

  $ perl -le '$i=0; print "\n\nNumber of command line arguments: ", scalar(@ARGV); map { print "argv[$i] = :", $ARGV[$i++], ":"; } @ARGV' '$FOO'
  
  
  Number of command line arguments: 1
  argv[0] = :$FOO:

#######################################################

  $ perl -le '$i=0; print "\n\nNumber of command line arguments: ", scalar(@ARGV); map { print "argv[$i] = :", $ARGV[$i++], ":"; } @ARGV' $FOO
  
  
  Number of command line arguments: 5
  argv[0] = :zero:
  argv[1] = :one:
  argv[2] = :two:
  argv[3] = :three:
  argv[4] = :four:

#######################################################

  $ perl -le '$i=0; print "\n\nNumber of command line arguments: ", scalar(@ARGV); map { print "argv[$i] = :", $ARGV[$i++], ":"; } @ARGV' "$FOO"
  
  
  Number of command line arguments: 1
  argv[0] = :zero one two three four:

#######################################################


There's another kind of quote too:  backquotes, `like these`.  These
are different than either single or double quotes too.

Perhaps the best place you could learn all of the rules here is the
bash man page.

Regards,

--kevin
-- 
GnuPG ID: B280F24E              Never could stand that dog.
alumni.unh.edu!kdc                   -- Tom Waits



More information about the gnhlug-discuss mailing list