Re: bash - quote removal question
From: Chris F.A. Johnson (c.fa.johnson_at_rogers.com)
Date: 01/30/04
- Next message: dave: "call a webpage"
- Previous message: Shaun: "Re: Simple join of 2 lines!!! Arrgh!"
- In reply to: nospam55: "bash - quote removal question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: dave: "call a webpage"
- Previous message: Shaun: "Re: Simple join of 2 lines!!! Arrgh!"
- In reply to: nospam55: "bash - quote removal question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|