alias rfw "perl -ne '/\$mail_cmd/ || print;' \!^"

You need quoting at several levels:

  1. When defining the alias any special characters that are supposed to be in the alias need protecting from being acted upon immediately.
  2. The alias needs to end up with enough quotes or escaping in it so that special characters get passed to Perl rather than being interpreted by the shell.
  3. Perl itself can need some things escaping.

Each level may add quoting characters which themselves need protecting at the previous level, so let's take them in reverse. Ignore aliasing and the shell for the moment, you want a Perl program that consists of:

/\$mail_cmd/ || print;

That backslash has to be there so that Perl treats the dollar sign as a literal dollar, rather than trying to interpolate a Perl variable. However the semicolon is unnecessary and the double bars can be replaced by something else, so let's get rid of those — quoting in C-Shell aliases is rather broken, so minimizing the number of characters that need protecting is a good start. This'll work:

print unless /\$mail_cmd/

That needs quoting as a whole for passing to the shell. It contains a dollar sign, so double quotes can't be used, because in the C Shell dollars in double quotes are always acted upon by the shell. So let's use single quotes for this:

perl -wne 'print unless /\$mail_cmd/'

Now we want to set up an alias with that as the value. Spaces in an alias definition don't usually matter: even if the entire definition is parsed as a single parameter now, it's going to get split on spaces when it gets expanded. The only special characters in the above which need protecting are the single quotes, the backslash, and the dollar. So one way of writing the alias is simply to stick a backslash before each of those characters (and before the exclamation mark in the alias's placeholder):

alias rfw perl -wne \'print unless /\\\$mail_cmd/\' \!^

Again because of the dollar sign there's no point in trying to use double quotes. But you could use single quotes to protect everything except for the single quotes. There's only one set of single quotes in the alias expansion, so you could put a new set of single quotes just inside them (to protect everything that's between them) and just put backslashes before each of the outer single quotes (to protect them):

alias rfw perl -wne \''print unless /\$mail_cmd/'\' \!^

Smylers


In reply to Re^4: csh, alias, perl & quotes by Smylers
in thread csh, alias, perl & quotes by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.