[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash Shell - escapes



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, May 10, 2016 at 11:20:23AM +0200, Die Optimisten wrote:
> On 2016-05-10 11:18, Die Optimisten wrote:
> >Hi,
> >
> >How can I escape a ' inside '...'
> >e.g. perl -e 'print '$ and a' '    # I don't want to use "

The short answer is... you can't. Quoting from the bash man page:

  "Enclosing characters in single quotes preserves the  literal  value  of
   each character within the quotes.  A single quote may not occur between
   single quotes, even when preceded by a backslash"

The long answer is... this woldn't be a shell if there were no useful
workarounds.

The trick in this situation is just to break up the string. Be aware
that to a shell, everything is text, so foo, "foo" and 'foo' are all
(in some way) equivalent.

This would work:

   perl -e 'print '"'"'$ and a'"'"' '    # I don't want to use "

That is, you glue your string out of several parts, here separated
by spaces; only the embedded ' are quoted by "":

   'print '  "'"  '$ and a'  "'"  ' '

Of course, in practice you wouldn't do that blind transform, but
optimize it a bit, e.g.:

   "print '"'$'" and ax ' "

I.e. just use the '' where you need 'em

Then, you can escape the $ whithin the "" with a \

Lots of ways, use whatever is most readable.

hth
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlcxtVcACgkQBcgs9XrR2kZmlgCffAw1D1VN5+S6M9QlMtzb+egm
9FcAn1VzNSS8Y0R+APMmc8r0fKUkIeV8
=F9uz
-----END PGP SIGNATURE-----


Reply to: