I ran into this problem while trying to print single quotes in a Perl one-liner. I eventually figured out you have to escape them with \'\\\'\'
. Here\'s some code t
To your shell, 'chomp; print "'$_'\n"'
results in a string that's the concatenation of
chomp; print "
(the first sequence inside single quotes),$_
, and\n"
(the second sequence inside single quotes).In bash
, $_
"... expands to the last argument to the previous command, after expansion. ...". Since this happens to be shortlist.txt
, the following is passed to perl
:
chomp; print "shortlist.txt\n"
For example,
$ echo foo
foo
$ echo 'chomp; print "'$_'\n"'
chomp; print "foo\n"
Note that the above mechanism shouldn't be used to pass values to a Perl one-liner. You shouldn't be generating Perl code from the shell. See How can I process options using Perl in -n or -p mode? for how to provide arguments to a one-liner.