Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #167652 > unrolled thread

Shell - escapes

Started byDie Optimisten <inform@die-optimisten.net>
First post2016-05-10 11:40 +0200
Last post2016-05-11 05:50 +0200
Articles 20 on this page of 22 — 11 participants

Back to article view | Back to linux.debian.user


Contents

  Shell - escapes Die Optimisten <inform@die-optimisten.net> - 2016-05-10 11:40 +0200
    bash Shell - escapes Die Optimisten <inform@die-optimisten.net> - 2016-05-10 11:40 +0200
      Re: bash Shell - escapes <tomas@tuxteam.de> - 2016-05-10 12:20 +0200
        Re: Shell - escapes "Thomas Schmitt" <scdbackup@gmx.net> - 2016-05-10 13:10 +0200
          Re: Shell - escapes <tomas@tuxteam.de> - 2016-05-10 13:30 +0200
    Re: Shell - escapes Mark Fletcher <mark27q1@gmail.com> - 2016-05-10 12:10 +0200
      Re: Shell - escapes "Thomas Schmitt" <scdbackup@gmx.net> - 2016-05-10 12:50 +0200
        Re: Shell - escapes Die Optimisten <inform@die-optimisten.net> - 2016-05-10 14:20 +0200
          Re: Shell - escapes <tomas@tuxteam.de> - 2016-05-10 14:30 +0200
            Re: Shell - escapes Mark Fletcher <mark27q1@gmail.com> - 2016-05-10 15:20 +0200
              Re: Shell - escapes David Wright <deblis@lionunicorn.co.uk> - 2016-05-10 16:00 +0200
              Re: Shell - escapes Mart van de Wege <mvdwege@gmail.com> - 2016-05-12 10:10 +0200
            Re: Shell - escapes tomas@tuxteam.de - 2016-05-10 15:30 +0200
          Re: Shell - escapes "Thomas Schmitt" <scdbackup@gmx.net> - 2016-05-10 16:20 +0200
            Re: Shell - escapes David Wright <deblis@lionunicorn.co.uk> - 2016-05-10 16:30 +0200
              Re: Shell - escapes "Thomas Schmitt" <scdbackup@gmx.net> - 2016-05-10 16:50 +0200
            Re: Shell - escapes <tomas@tuxteam.de> - 2016-05-10 16:40 +0200
    Re: Shell - escapes shawn wilson <ag4ve.us@gmail.com> - 2016-05-10 12:20 +0200
    Re: Shell - escapes Frédéric Marchal <frederic.marchal@wowtechnology.com> - 2016-05-10 12:30 +0200
    Re: Shell - escapes Andy Smith <andy@strugglers.net> - 2016-05-10 15:40 +0200
      Re: Shell - escapes <tomas@tuxteam.de> - 2016-05-10 16:00 +0200
      Re: Shell - escapes David Christensen <dpchrist@holgerdanske.com> - 2016-05-11 05:50 +0200

Page 1 of 2  [1] 2  Next page →


#167652 — Shell - escapes

FromDie Optimisten <inform@die-optimisten.net>
Date2016-05-10 11:40 +0200
SubjectShell - escapes
Message-ID<rxcj0-8b4-13@gated-at.bofh.it>
Hi,

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

thank you
Andrew

[toc] | [next] | [standalone]


#167653 — bash Shell - escapes

FromDie Optimisten <inform@die-optimisten.net>
Date2016-05-10 11:40 +0200
Subjectbash Shell - escapes
Message-ID<rxcj1-8b4-25@gated-at.bofh.it>
In reply to#167652

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [prev] | [next] | [standalone]


#167659 — Re: bash Shell - escapes

From<tomas@tuxteam.de>
Date2016-05-10 12:20 +0200
SubjectRe: bash Shell - escapes
Message-ID<rxcVI-yd-21@gated-at.bofh.it>
In reply to#167653
-----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-----

[toc] | [prev] | [next] | [standalone]


#167665

From"Thomas Schmitt" <scdbackup@gmx.net>
Date2016-05-10 13:10 +0200
Message-ID<rxdI6-1ir-17@gated-at.bofh.it>
In reply to#167659
Hi,

tomas@tuxteam.de 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

[toc] | [prev] | [next] | [standalone]


#167667

From<tomas@tuxteam.de>
Date2016-05-10 13:30 +0200
Message-ID<rxe1t-1qz-31@gated-at.bofh.it>
In reply to#167665
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, May 10, 2016 at 01:08:04PM +0200, Thomas Schmitt wrote:
> Hi,
> 
> tomas@tuxteam.de 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-----

[toc] | [prev] | [next] | [standalone]


#167656

FromMark Fletcher <mark27q1@gmail.com>
Date2016-05-10 12:10 +0200
Message-ID<rxcM2-qT-17@gated-at.bofh.it>
In reply to#167652

[Multipart message — attachments visible in raw view] — view raw

On Tue, 10 May 2016 at 18:36, Die Optimisten <inform@die-optimisten.net>
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

[toc] | [prev] | [next] | [standalone]


#167663

From"Thomas Schmitt" <scdbackup@gmx.net>
Date2016-05-10 12:50 +0200
Message-ID<rxdoJ-P9-3@gated-at.bofh.it>
In reply to#167656
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

[toc] | [prev] | [next] | [standalone]


#167668

FromDie Optimisten <inform@die-optimisten.net>
Date2016-05-10 14:20 +0200
Message-ID<rxeNQ-2bK-11@gated-at.bofh.it>
In reply to#167663
 > 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

[toc] | [prev] | [next] | [standalone]


#167670

From<tomas@tuxteam.de>
Date2016-05-10 14:30 +0200
Message-ID<rxeXv-2hn-1@gated-at.bofh.it>
In reply to#167668
-----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-----

[toc] | [prev] | [next] | [standalone]


#167672

FromMark Fletcher <mark27q1@gmail.com>
Date2016-05-10 15:20 +0200
Message-ID<rxfJU-32e-9@gated-at.bofh.it>
In reply to#167670

[Multipart message — attachments visible in raw view] — view raw

On Tue, May 10, 2016 at 9:20 PM <tomas@tuxteam.de> 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.

[toc] | [prev] | [next] | [standalone]


#167678

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2016-05-10 16:00 +0200
Message-ID<rxgmD-3mZ-41@gated-at.bofh.it>
In reply to#167672
On Tue 10 May 2016 at 13:16:27 (+0000), Mark Fletcher wrote:
> On Tue, May 10, 2016 at 9:20 PM <tomas@tuxteam.de> 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.

[toc] | [prev] | [next] | [standalone]


#167766

FromMart van de Wege <mvdwege@gmail.com>
Date2016-05-12 10:10 +0200
Message-ID<rxTQZ-1yW-11@gated-at.bofh.it>
In reply to#167672
Mark Fletcher <mark27q1@gmail.com> writes:

> On Tue, May 10, 2016 at 9:20 PM <tomas@tuxteam.de> 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.
>
"Doctor, it hurts when I do this."

"Well, don't do that, then!"

Mart

-- 
"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.

[toc] | [prev] | [next] | [standalone]


#167673

Fromtomas@tuxteam.de
Date2016-05-10 15:30 +0200
Message-ID<rxfTA-38S-17@gated-at.bofh.it>
In reply to#167670
-----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 <tomas@tuxteam.de> 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-----

[toc] | [prev] | [next] | [standalone]


#167679

From"Thomas Schmitt" <scdbackup@gmx.net>
Date2016-05-10 16:20 +0200
Message-ID<rxgFZ-3QP-29@gated-at.bofh.it>
In reply to#167668
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

[toc] | [prev] | [next] | [standalone]


#167680

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2016-05-10 16:30 +0200
Message-ID<rxgPE-3Xe-21@gated-at.bofh.it>
In reply to#167679
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.

[toc] | [prev] | [next] | [standalone]


#167683

From"Thomas Schmitt" <scdbackup@gmx.net>
Date2016-05-10 16:50 +0200
Message-ID<rxh90-4bJ-1@gated-at.bofh.it>
In reply to#167680
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

[toc] | [prev] | [next] | [standalone]


#167681

From<tomas@tuxteam.de>
Date2016-05-10 16:40 +0200
Message-ID<rxgZk-42L-31@gated-at.bofh.it>
In reply to#167679
-----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-----

[toc] | [prev] | [next] | [standalone]


#167658

Fromshawn wilson <ag4ve.us@gmail.com>
Date2016-05-10 12:20 +0200
Message-ID<rxcVI-yd-5@gated-at.bofh.it>
In reply to#167652

[Multipart message — attachments visible in raw view] — view raw

'...'  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" <inform@die-optimisten.net> wrote:

> Hi,
>
> How can I escape a ' inside '...'
> e.g. perl -e 'print '$ and a' '    # I don't want to use "
>
> thank you
> Andrew
>
>

[toc] | [prev] | [next] | [standalone]


#167661

FromFrédéric Marchal <frederic.marchal@wowtechnology.com>
Date2016-05-10 12:30 +0200
Message-ID<rxd5p-FH-33@gated-at.bofh.it>
In reply to#167652
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

[toc] | [prev] | [next] | [standalone]


#167674

FromAndy Smith <andy@strugglers.net>
Date2016-05-10 15:40 +0200
Message-ID<rxg3f-3eA-19@gated-at.bofh.it>
In reply to#167652

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | linux.debian.user


csiph-web