help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Expansions in associative array subscripts


From: Stephane Chazelas
Subject: Re: [Help-bash] Expansions in associative array subscripts
Date: Wed, 7 Aug 2019 17:00:06 +0100
User-agent: NeoMutt/20171215

2019-08-08 01:34:45 +1000, David:
[...]
> > As I showed earlier. Exactly the same as "${foo/bar/"$baz"}" or
> > "$(cmd1 "$arg1")" where quotes nest because of syntax in between the
> > sets of quotes.
> 
> Wow, thank you for that sentence, that right there is what clarified this
> for me. I was completely unaware that the double quotes might nest.
> So with that new understanding, my other related questions fade away.
[...]

Note that it was not the case in the Bourne shell or ksh88
(which bash is based upon).

On Solaris 10:

$ ksh -c 'echo "${0+" x|y "}"'
ksh: "${0+": bad substitution
ksh: y:  not found
$ /bin/sh -c 'echo "${0+" x|y "}"'
/bin/sh: bad substitution
/bin/sh: y: not found

In that case, for portability, you may prefer:

$ /bin/sh -c 'echo "${0+ x|y }"'
 x|y
$ ksh -c 'echo "${0+ x|y }"'
 x|y
$ bash -c 'echo "${0+ x|y }"'
 x|y

Note that in the case of ksh88 at least, the problem is at the
tokenisation stage. In cases where it manages to identify the
${...} as a full token, the two layers of quotes are processed
correctly:

$ a=abc* b=* ksh -c 'IFS=b; echo "${a%%$b}"'

$ a=abc* b=* ksh -c 'IFS=b; echo "${a%%"$b"}"'
abc

The outer quotes prevent IFS-splitting, the inner quotes prevent
the * from being taken as a pattern.

$ a='abc|*' b=* ksh -c 'echo "${a%%"|$b"}"'
ksh: "${a%%": bad substitution
ksh: *}:  not found

Tokeniser saw "${a%%", | and "$b"}" tokens.

$ a='abc|*' b=* ksh -c 'echo "${a%%"\|$b"}"'
abc|*

Didn't work there, the \ helped the tokeniser, but then that \
remained and prevented the substitution

$ a='abc|*' b=* ksh -c 'echo "${a%%|"$b"}"'
abc|

doesn't work either. You need

$ a='abc|*' b=* ksh -c 'echo "${a%%\|"$b"}"'
abc

That behaviour of ksh88 has been rejected by POSIX as
non-compliant.

Still, the POSIX sh of several commercial Unices (like
/usr/xpg4/bin/sh on Solaris) is still based on ksh88 and still
haven't been fixed.

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]