Re: bash - quote removal question

From: Chris F.A. Johnson (c.fa.johnson_at_rogers.com)
Date: 01/30/04


Date: 30 Jan 2004 19:30:44 GMT

On Fri, 30 Jan 2004 at 08:01 GMT, nospam55 wrote:
> Hi. I was trying to see how to detach colon-separated fields in a scalar var;
> apart from the classical loops under IFS=:, the nicest ways I found are
>
> 1. literally newline inside ${ // / } inside weak quotes:
>
> bash$ foo='a b:c'
> bash$ echo "${foo//:/
> }"
> a b
> c
> bash$
>
> 2. piping to tr : $'\n'
> bash$ echo "$foo" | tr : $'\n'
> a b
> c
> bash$

     I use:

printf "%s\n" "${foo//:/ }"

> Now comes the question : I wanted to say $'\n' instead of a literal newline;
> by sort of merging the former examples I tried
>
> 3. "${foo//:/$'\n'}", i.e. :
>
> bash$ foo=dog:cat:monkey
> bash$ echo "${foo//:/$'\n'}"
> dog'
> 'cat'
> 'monkey
> bash$
>
> the surprising result is that in the output only the outer (before dog and after
> monkey) strong quotes have been removed.
>
> Does anybody figure out why? I suppose that a relevant section in the bash manpage is :
>
> Quote Removal
> After the preceding expansions, all unquoted occurrences of the charac?
> ters \, ?, and " that did not result from one of the above expansions
> [i.e. essentially brace- tilde- parameter- cmd- arithmetic- pathname-
> expansion, and word splitting] are removed.
>
> If this does matter, then maybe a useful question could be : how does bash mark
> and remember which quotes did result from that expansions?

   The relevant section of the man page is:

==========================
QUOTING
       .......
       Words of the form $'string' are treated specially. The word expands to
       .......
       The expanded result is single-quoted, as if the dollar sign had not
       been present.
==========================

   I would set a variable:

NL=$'\n'
echo "${foo//:/$NL}"

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages

  • Re: Dog Question for Cohen
    ... >>> Maybe rig up a quick release so you can let the dog go chase whatever, ... Or the top is trying to go sideways and the bottom ... If they didn't have the quotes, I would expect a safety release. ...
    (misc.fitness.weights)
  • Re: Creative use of a webcam
    ... I have voiced this gripe before and I will continue to voice this gripe and netiquette or not, I will continue to post above the quotes. ... standing there right in front of the front door. ... I started up the stairs, saw her standing there wagging her tail, and there I saw Woodchuck #4, just grinning at us and acting like he had no idea that he is supposed to hide from me. ... Dog never barks, is not one to defend us... ...
    (sci.med.transcription)
  • Re: Dog Question for Cohen
    ... >>>the dog sees a cat off to the side. ... one second you're riding along and the next your bicycle is trying ... >If they didn't have the quotes, I would expect a safety release. ...
    (misc.fitness.weights)
  • Re: Follow up to: "Ecclesial Communion, Conciliarity and Authority"
    ... makes it clear that those who are outside the Roman Church are sinking ... But there is no dog barking. ... include the context back in, and these quotes don't in fact say what ...
    (uk.religion.christian)
  • bash - quote removal question
    ... apart from the classical loops under IFS=:, the nicest ways I found are ... monkey) strong quotes have been removed. ... I suppose that a relevant section in the bash manpage is: ... then maybe a useful question could be: ...
    (comp.unix.shell)