Shell - escapes

classic Classic list List threaded Threaded
22 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Shell - escapes

Die Optimisten
Hi,

How can I escape a ' inside '...'
e.g. perl -e 'print '$ and a' '    # I don't want to use "

thank you
Andrew

Reply | Threaded
Open this post in threaded view
|

bash Shell - escapes

Die Optimisten
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 "

thank you
Andrew

I have to add, its bash - specific
and PLEASE also CC: me using  inform (AT) die-optimisten.net
I'm not subscribed here
THANKs
Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

Mark Fletcher-2
In reply to this post by Die Optimisten

On Tue, 10 May 2016 at 18:36, Die Optimisten <[hidden email]> wrote:
Hi,

How can I escape a ' inside '...'
e.g. perl -e 'print '$ and a' '    # I don't want to use "

thank you
Andrew

perl -e 'print '\''$ and a'\'' '

The things that might look like double quotes in the above depending on your font are actually two single quotes side by side.

Mark
Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

shawn wilson
In reply to this post by Die Optimisten

'...'  doesn't interpolate.
push @f, '$ and a';
push @f, "'";
print join '', @f;
If you want. I have a feeling YDIW and need to step back and present the actual problem.

On May 10, 2016 05:36, "Die Optimisten" <[hidden email]> wrote:
Hi,

How can I escape a ' inside '...'
e.g. perl -e 'print '$ and a' '    # I don't want to use "

thank you
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: bash Shell - escapes

tomas@tuxteam.de
In reply to this post by Die Optimisten
-----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 | Threaded
Open this post in threaded view
|

Re: Shell - escapes

Frédéric Marchal
In reply to this post by Die Optimisten
On Tuesday 10 May 2016 11:18:06 Die Optimisten wrote:
> Hi,
>
> How can I escape a ' inside '...'
> e.g. perl -e 'print '$ and a' '    # I don't want to use "

This seems to work:

    perl -e 'print '\''$ and a'\'

It must be understood as the concatenation of these strings:

* literal string: 'print '
* escaped \'
* literal string: '$ and a'
* escaped \'

Frederic

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

Thomas Schmitt
In reply to this post by Mark Fletcher-2
Hi,

Die Optimisten wrote:
> > How can I escape a ' inside '...'
> > e.g. perl -e 'print '$ and a' '

Mark Fletcher wrote:
> perl -e 'print '\''$ and a'\'' '

Or by ending the range of ' and packing the literal ' into
double quotes:

  perl -e 'print '"'"'$ and a'"'"' '

consisting of these quotation pieces

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

If there was no $ in the text, one could do it more simply by
packing the whole text into double quotes:

  perl -e "print '$ and a' "


Have a nice day :)

Thomas

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

Thomas Schmitt
In reply to this post by tomas@tuxteam.de
Hi,

[hidden email] wrote:
>    perl -e 'print '"'"'$ and a'"'"' '    # I don't want to use "

You were faster than me. :))


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

I actually do it vice versa:
If purely literal text is intended, i use '' where possible and
escape only '.

That's most safe because i do not have to ponder which character
is interpreted and converted inside "" and which will stay as is.
So it is also easy to program in C.


Have a nice day :)

Thomas

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

tomas@tuxteam.de
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, May 10, 2016 at 01:08:04PM +0200, Thomas Schmitt wrote:

> Hi,
>
> [hidden email] wrote:
> >    perl -e 'print '"'"'$ and a'"'"' '    # I don't want to use "
>
> You were faster than me. :))
>
>
> > I.e. just use the '' where you need 'em
>
> I actually do it vice versa:
> If purely literal text is intended, i use '' where possible and
> escape only '.
>
> That's most safe because i do not have to ponder which character
> is interpreted and converted inside "" and which will stay as is.
> So it is also easy to program in C.

Good insight. This was my first cut as well, but I came from a
slightly different angle: starting from '...', what would an
automatic transformation look like?

> Have a nice day :)

same to you :-)

- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlcxxbQACgkQBcgs9XrR2kb7cACfXYcRaA+YSDCLzaQCL6S8sJFw
b4wAni2zG9YVvh0LoCyn/GyWJ8xs6LgI
=/y2Z
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

Die Optimisten
In reply to this post by Thomas Schmitt
 > If there was no $ in the text, one could do it more simply by packing
the whole text into double quotes:
perl -e "print '$ and a' "
 > Have a nice day :) Thomas

That's why I constructed that example :)

I think it would be useful to have a (new, meta) quote, which fully
hides contents from bash-interpretion
so that (perl-)strings can be built without thinking of quoting/escaping.
Would it be useful / possible to change '...' so that nothing _except_
\'  ist interpreted, I think that would be a nice solution --- or use a
new (unused???) character.
- Is it worth to forward it to the bash-experts (perhaps sb of them
reading this...)

Thanks for that many answers!
Andrew


Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

tomas@tuxteam.de
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, May 10, 2016 at 01:54:35PM +0200, Die Optimisten wrote:

> > If there was no $ in the text, one could do it more simply by
> packing the whole text into double quotes:
> perl -e "print '$ and a' "
> > Have a nice day :) Thomas
>
> That's why I constructed that example :)
>
> I think it would be useful to have a (new, meta) quote, which fully
> hides contents from bash-interpretion
> so that (perl-)strings can be built without thinking of quoting/escaping.
> Would it be useful / possible to change '...' so that nothing
> _except_ \'  ist interpreted, I think that would be a nice solution
> --- or use a new (unused???) character.
> - Is it worth to forward it to the bash-experts (perhaps sb of them
> reading this...)

You can try that, but be warned that shell (Unix) syntax in general and
bash syntax in particular is a rather mature affair, result of many
shaping forces (esp. compatibility to other shells and backward compatibility
considerations). At this point in history it has achieved an exquisite
equilibrium and there's a huge body of scripts to cater for.

So motivation to change syntax at such a basic level is probably
pretty low (and with a reason).

If you are embedding longer scripts in your shell, consider using
"here documents", which are more flexible wrt. embedded quotes.
For one-liners, Thomas' solution works nicely.

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

iEYEARECAAYFAlcx0gwACgkQBcgs9XrR2kb6TACfYgdtxEWKUq63xrKc8E53Gd/b
QHoAn0/3IB0YTKnmSKVV6LDx6sFwR97A
=KOjh
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

Mark Fletcher-2


On Tue, May 10, 2016 at 9:20 PM <[hidden email]> wrote:

If you are embedding longer scripts in your shell, consider using
"here documents", which are more flexible wrt. embedded quotes.
For one-liners, Thomas' solution works nicely.

Except that it does what the OP clearly said he does NOT want to do -- it uses double quotes.

 
Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

tomas@tuxteam.de
In reply to this post by tomas@tuxteam.de
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, May 10, 2016 at 01:14:57PM +0000, Mark Fletcher wrote:

> On Tue, May 10, 2016 at 9:20 PM <[hidden email]> wrote:
>
> >
> > If you are embedding longer scripts in your shell, consider using
> > "here documents", which are more flexible wrt. embedded quotes.
> > For one-liners, Thomas' solution works nicely.
> >
> >
> Except that it does what the OP clearly said he does NOT want to do -- it
> uses double quotes.

I think in the thread it's explained nicely why it can't work with
single quotes alone. For the non-single-quoted stretches you can
of course do without double quotes (use backslash), but I think
we are splitting hairs at this point.


Thomas and me just guessed at the OP's intentions: (s)he didn't
want double quotes to protect the $ in there. I think we guessed
right :-)

regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlcx4JQACgkQBcgs9XrR2kZpjQCffzfOP5OuC771S28FItQjFus0
3ecAmgMIILeAmt1TdBuhVnXJHhDOMrjL
=I5y2
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

andy smith-10
In reply to this post by Die Optimisten
Hello,

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

You can't, so if it were me I would use one of perl's alternatives
for single-quoted strings, such as:

perl -e 'print q{$ and a} '

http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators

Cheers,
Andy

--
http://bitfolk.com/ -- No-nonsense VPS hosting

> The optimum programming team size is 1.
Has Jurassic Park taught us nothing?         — pfilandr

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

tomas@tuxteam.de
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, May 10, 2016 at 01:36:22PM +0000, Andy Smith wrote:

> Hello,
>
> On Tue, May 10, 2016 at 11:18:06AM +0200, Die Optimisten wrote:
> > How can I escape a ' inside '...'
> > e.g. perl -e 'print '$ and a' '    # I don't want to use "
>
> You can't, so if it were me I would use one of perl's alternatives
> for single-quoted strings, such as:
>
> perl -e 'print q{$ and a} '
>
> http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators

"There Is More Than a Way To Do It" :-)

By the way, this is recommended reading (back to the shell and
especially bash):

  http://wiki.bash-hackers.org/

Enjoy
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlcx538ACgkQBcgs9XrR2ka7GgCfd2N8PaoE2ng7i4hPuASpH+up
/6IAmwSHaxmQVI9f9s3DhJAmARPHoPVw
=k8A/
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

David Wright-3
In reply to this post by Mark Fletcher-2
On Tue 10 May 2016 at 13:16:27 (+0000), Mark Fletcher wrote:
> On Tue, May 10, 2016 at 9:20 PM <[hidden email]> wrote:
> > If you are embedding longer scripts in your shell, consider using
> > "here documents", which are more flexible wrt. embedded quotes.
> > For one-liners, Thomas' solution works nicely.
> >
> Except that it does what the OP clearly said he does NOT want to do -- it
> uses double quotes.

It appears to me that the OP included that condition in order to
persuade us that:
(a) a new quoting mechanism is needed for building perl strings in shell:
https://lists.debian.org/debian-user/2016/05/msg00403.html
and (b) an old quoting mechanism should be changed:
https://lists.debian.org/debian-user/2016/05/msg00388.html

You can call me cynical if you wish.

Cheers,
David.

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

Thomas Schmitt
In reply to this post by Die Optimisten
Hi,

> I think it would be useful to have a (new, meta) quote, which fully hides
> contents from bash-interpretion

The '' quote does this. It's simply impossible that an end quotation mark
can be distinguished from a literal quotation mark.
If there would be escaping of literal string end marks, then you get even
more interpretation and conversion.

So the solution with two kinds of quotation marks is the most simple,
if you do not want to go to FORTRAN strings with length number and
Hollerith constant:  5Hhello

Unambiguous, ingenious, uncomfortable.


The best alternative i know of would be an adjustable end mark.
Before the string begins, one would adjust the environment to a character
or string which surely is not part of the literal text.

The Here Documents of the shell are an example of this design pattern.
Get some unlikely text:

  $ uuidgen
  2bf661c8-b400-43f3-addc-d2d75f018013

Announce and use it as delimiter:

  $ wc <<2bf661c8-b400-43f3-addc-d2d75f018013
  ... text ...
  2bf661c8-b400-43f3-addc-d2d75f018013

Regrettably, Here Documents let the shell fiddle with their text.

  $ wc -c <<x
  $(echo hello)
  x
  6

So this is not a way to express arbitrary literal text.


> Would it be useful / possible to change

One can switch from one shell to the other, one can even program an
own shell. But i see very few chance that bash or dash upstream would
accept any change or addition attempt about quoting. It is just too
fundamental and there are viable ways to express a literal string.


Have a nice day :)

Thomas

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

David Wright-3
On Tue 10 May 2016 at 16:19:10 (+0200), Thomas Schmitt wrote:

> Regrettably, Here Documents let the shell fiddle with their text.
>
>   $ wc -c <<x
>   $(echo hello)
>   x
>   6
>
> So this is not a way to express arbitrary literal text.

$ wc -c <<"x"
$(echo hello)
x

14
$

Cheers,
David.

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

tomas@tuxteam.de
In reply to this post by Thomas Schmitt
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, May 10, 2016 at 04:19:10PM +0200, Thomas Schmitt wrote:

> Regrettably, Here Documents let the shell fiddle with their text.
>
>   $ wc -c <<x
>   $(echo hello)
>   x
>   6
>
> So this is not a way to express arbitrary literal text.

Not if you quote the delimiter cookie in single quotes (in bash,
at least):

  | cat <<EOT
  | the current shell is $SHELL, I think
  | EOT
  |
  | => the current shell is /bin/bash, I think

but:

  | cat <<'EOT'
  | the current shell is $SHELL, I think
  | EOT
  |
  | => the current shell is $SHELL, I think

A bit like Perl (or was it the other way 'round? ;-)

> > Would it be useful / possible to change
>
> One can switch from one shell to the other, one can even program an
> own shell. But i see very few chance that bash or dash upstream would
> accept any change or addition attempt about quoting. It is just too
> fundamental and there are viable ways to express a literal string.

That's my take too. And I think the decision to move slowly, if ever
does make a ton of sense in this context.

regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlcx8MgACgkQBcgs9XrR2kZ3LwCdHCYhbwBUo5KlgsZJoxPOfuXO
p58AnR+iq50k4Hk61RY59/0Fn4boEs64
=IIYV
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Shell - escapes

Thomas Schmitt
In reply to this post by David Wright-3
Hi,

i wrote:
> > So this is not a way to express arbitrary literal text.

David Wright wrote:
> $ wc -c <<"x"

Indeed. One more way to reach the goal.
At least with bash and dash.


Have a nice day :)

Thomas

12